Skip to content

Commit

Permalink
add ut for spinlock
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkiller committed Oct 16, 2023
1 parent 27ab88c commit 3145271
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions spinlock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package rollingwriter

import (
"sync"
"testing"
)

func TestSpinLock(t *testing.T) {
var counter int
var wg sync.WaitGroup
spinLock := &Locker{}

startCh := make(chan struct{})

wg.Add(5000)
for i := 0; i < 5000; i++ {
go func() {
defer wg.Done()

// waitting start
<-startCh

for j := 0; j < 10000; j++ {
spinLock.Lock()
counter++
spinLock.Unlock()
}
}()
}

close(startCh)
wg.Wait()

if counter != 50000000 {
t.Errorf("Counter should be 50000000, but got %d", counter)
}
}

0 comments on commit 3145271

Please sign in to comment.