|
| 1 | +package quickfix |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "runtime/pprof" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/quickfixgo/quickfix/internal" |
| 11 | +) |
| 12 | + |
| 13 | +type testLog struct{} |
| 14 | + |
| 15 | +func (testLog) OnIncoming([]byte) {} |
| 16 | +func (testLog) OnOutgoing([]byte) {} |
| 17 | +func (testLog) OnEvent(string) {} |
| 18 | +func (testLog) OnEventf(string, ...interface{}) {} |
| 19 | + |
| 20 | +// testApp is a no-op Application for tests. |
| 21 | +type testApp struct{} |
| 22 | + |
| 23 | +func (testApp) OnCreate(SessionID) {} |
| 24 | +func (testApp) OnLogon(SessionID) {} |
| 25 | +func (testApp) OnLogout(SessionID) {} |
| 26 | +func (testApp) ToAdmin(*Message, SessionID) {} |
| 27 | +func (testApp) FromAdmin(*Message, SessionID) MessageRejectError { return nil } |
| 28 | +func (testApp) ToApp(*Message, SessionID) error { return nil } |
| 29 | +func (testApp) FromApp(*Message, SessionID) MessageRejectError { return nil } |
| 30 | + |
| 31 | +func newTimerOnlySession() *session { |
| 32 | + tr, _ := internal.NewUTCTimeRange(internal.NewTimeOfDay(0, 0, 0), internal.NewTimeOfDay(23, 59, 59), nil) |
| 33 | + s := &session{ |
| 34 | + store: &memoryStore{}, |
| 35 | + log: testLog{}, |
| 36 | + sessionID: SessionID{BeginString: BeginStringFIX44, SenderCompID: "S", TargetCompID: "T"}, |
| 37 | + messageOut: make(chan []byte, 2), |
| 38 | + messageIn: make(chan fixIn), |
| 39 | + sessionEvent: make(chan internal.Event), |
| 40 | + messageEvent: make(chan bool), |
| 41 | + application: testApp{}, |
| 42 | + stopCh: make(chan struct{}), |
| 43 | + SessionSettings: internal.SessionSettings{ |
| 44 | + SessionTime: tr, |
| 45 | + }, |
| 46 | + } |
| 47 | + return s |
| 48 | +} |
| 49 | + |
| 50 | +func countGoroutinesContaining(substr string) int { |
| 51 | + var buf bytes.Buffer |
| 52 | + _ = pprof.Lookup("goroutine").WriteTo(&buf, 2) |
| 53 | + return strings.Count(buf.String(), substr) |
| 54 | +} |
| 55 | + |
| 56 | +func TestLogonTimeoutDoesNotLeakGoroutine(t *testing.T) { |
| 57 | + s := newTimerOnlySession() |
| 58 | + s.InitiateLogon = true |
| 59 | + s.LogonTimeout = 10 * time.Millisecond |
| 60 | + |
| 61 | + baseline := countGoroutinesContaining("stateMachine).Connect.func1") |
| 62 | + |
| 63 | + s.stateMachine.Connect(s) |
| 64 | + close(s.stopCh) |
| 65 | + time.Sleep(4 * s.LogonTimeout) |
| 66 | + |
| 67 | + if got := countGoroutinesContaining("stateMachine).Connect.func1"); got > baseline { |
| 68 | + t.Fatalf("logon timeout goroutine leaked: baseline=%d current=%d", baseline, got) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestLogoutTimeoutDoesNotLeakGoroutine(t *testing.T) { |
| 73 | + s := newTimerOnlySession() |
| 74 | + s.LogoutTimeout = 10 * time.Millisecond |
| 75 | + baseline := countGoroutinesContaining("initiateLogoutInReplyTo") |
| 76 | + s.stateMachine.Connect(s) |
| 77 | + |
| 78 | + if err := s.initiateLogout("bye"); err != nil { |
| 79 | + t.Fatalf("initiateLogout returned error: %v", err) |
| 80 | + } |
| 81 | + close(s.stopCh) |
| 82 | + time.Sleep(4 * s.LogoutTimeout) |
| 83 | + |
| 84 | + if got := countGoroutinesContaining("initiateLogoutInReplyTo"); got > baseline { |
| 85 | + t.Fatalf("logout timeout goroutine leaked: baseline=%d current=%d", baseline, got) |
| 86 | + } |
| 87 | +} |
0 commit comments