Skip to content

Commit e35609a

Browse files
author
Aaron Schlesinger
committed
fix(pkg/sshd/circuit_test.go): add concurrency test for the circuit
1 parent 96ec946 commit e35609a

1 file changed

Lines changed: 33 additions & 1 deletion

File tree

pkg/sshd/circuit_test.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
package sshd
22

33
import (
4+
"math/rand"
5+
"sync"
6+
"sync/atomic"
47
"testing"
8+
"time"
59
)
610

11+
const (
12+
numConcurrents = 1000
13+
)
14+
15+
func init() {
16+
rand.Seed(time.Now().UnixNano())
17+
}
18+
719
func TestOpenCloseSerial(t *testing.T) {
820
c := NewCircuit()
921
if c.State() != OpenState {
@@ -24,5 +36,25 @@ func TestOpenCloseSerial(t *testing.T) {
2436
}
2537

2638
func TestOpenCloseConcurrent(t *testing.T) {
27-
39+
c := NewCircuit()
40+
var wg sync.WaitGroup
41+
state := uint32(0)
42+
for i := 0; i < numConcurrents; i++ {
43+
wg.Add(1)
44+
go func(i int) {
45+
defer wg.Done()
46+
r := rand.Int() % 2
47+
if r == 0 {
48+
c.Open()
49+
atomic.StoreUint32(&state, OpenState.toUint32())
50+
} else {
51+
c.Close()
52+
atomic.StoreUint32(&state, ClosedState.toUint32())
53+
}
54+
}(i)
55+
}
56+
wg.Wait()
57+
if state != c.State().toUint32() {
58+
t.Fatalf("expected state %d wasn't equal to actual %d", state, c.State().toUint32())
59+
}
2860
}

0 commit comments

Comments
 (0)