File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package sshd
22
33import (
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+
719func TestOpenCloseSerial (t * testing.T ) {
820 c := NewCircuit ()
921 if c .State () != OpenState {
@@ -24,5 +36,25 @@ func TestOpenCloseSerial(t *testing.T) {
2436}
2537
2638func 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}
You can’t perform that action at this time.
0 commit comments