Created
August 8, 2019 17:48
-
-
Save rkuzner/9cac3ca6963f47824292227e90ba1e06 to your computer and use it in GitHub Desktop.
Workshop Resiliencia - Demo Concurrencia
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package contador_test | |
import ( | |
"fmt" | |
"sync" | |
"testing" | |
"time" | |
) | |
// TestContadorConcurrenteConWaitGroup incrementa un contador con goroutines y waitgroup | |
func TestContadorConcurrenteConWaitGroup(t *testing.T) { | |
var contador int64 | |
var wg sync.WaitGroup | |
for i := 0; i < Limite; i++ { | |
wg.Add(1) | |
go func() { | |
defer wg.Done() | |
time.Sleep(CargaDeTrabajo) | |
contador++ | |
}() | |
} | |
wg.Wait() | |
if contador != Limite { | |
t.Errorf("el contador NO alcanzó el límite: %d", contador) | |
} else { | |
fmt.Println(fmt.Sprintf("%s: el contador alcanzo el límite!", t.Name())) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment