Skip to content

Instantly share code, notes, and snippets.

@rkuzner
Last active August 8, 2019 17:56
Show Gist options
  • Save rkuzner/fe84746031721ff256011f11c99d0af4 to your computer and use it in GitHub Desktop.
Save rkuzner/fe84746031721ff256011f11c99d0af4 to your computer and use it in GitHub Desktop.
Workshop Resiliencia - Demo Concurrencia
package contador_test
import (
"fmt"
"sync"
"sync/atomic"
"testing"
"time"
)
// TestContadorConcurrenteConWaitGroupConMutex incrementa un contador con goroutines, waitgroup y mutex
func TestContadorConcurrenteConWaitGroupConMutex(t *testing.T) {
var contador int64
var wg sync.WaitGroup
var mu sync.Mutex
for i := 0; i < Limite; i++ {
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(CargaDeTrabajo)
mu.Lock()
contador++
mu.Unlock()
}()
}
wg.Wait()
if contador != Limite {
t.Errorf("el contador debía llegara a %d, y llegó a: %d", Limite, contador)
} else {
fmt.Println(fmt.Sprintf("%s: el contador alcanzo el límite: %d", t.Name(), contador))
}
}
// TestContadorConcurrenteConWaitGroupConAtomic incrementa un contador con goroutines, waitgroup y atomic wrappers
func TestContadorConcurrenteConWaitGroupConAtomic(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)
atomic.AddInt64(&contador, 1)
}()
}
wg.Wait()
if contador != Limite {
t.Errorf("el contador debía llegara a %d, y llegó a: %d", Limite, contador)
} else {
fmt.Println(fmt.Sprintf("%s: el contador alcanzo el límite: %d", t.Name(), contador))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment