Skip to content

Instantly share code, notes, and snippets.

@rkuzner
Created August 8, 2019 17:48
Show Gist options
  • Save rkuzner/9cac3ca6963f47824292227e90ba1e06 to your computer and use it in GitHub Desktop.
Save rkuzner/9cac3ca6963f47824292227e90ba1e06 to your computer and use it in GitHub Desktop.
Workshop Resiliencia - Demo Concurrencia
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