func (s *SessionFactorySuite) TestStartAndEndTime() {
	s.SessionSettings.Set(config.StartTime, "12:00:00")
	s.SessionSettings.Set(config.EndTime, "14:00:00")
	session, err := s.newSession(s.SessionID, s.MessageStoreFactory, s.SessionSettings, s.LogFactory, s.App)
	s.Nil(err)
	s.NotNil(session.SessionTime)

	s.Equal(
		*internal.NewUTCTimeRange(internal.NewTimeOfDay(12, 0, 0), internal.NewTimeOfDay(14, 0, 0)),
		*session.SessionTime,
	)
}
Example #2
0
func (s *SessionSuite) TestCheckSessionTimeNotInRange() {
	var tests = []struct {
		before           sessionState
		initiateLogon    bool
		expectOnLogout   bool
		expectSendLogout bool
	}{
		{before: latentState{}},
		{before: logonState{}},
		{before: logonState{}, initiateLogon: true, expectOnLogout: true},
		{before: logoutState{}, expectOnLogout: true},
		{before: inSession{}, expectOnLogout: true, expectSendLogout: true},
		{before: resendState{}, expectOnLogout: true, expectSendLogout: true},
		{before: pendingTimeout{resendState{}}, expectOnLogout: true, expectSendLogout: true},
		{before: pendingTimeout{inSession{}}, expectOnLogout: true, expectSendLogout: true},
		{before: notSessionTime{}},
	}

	for _, test := range tests {
		s.SetupTest()
		s.session.State = test.before
		s.session.InitiateLogon = test.initiateLogon
		s.Nil(s.session.store.IncrNextSenderMsgSeqNum())
		s.Nil(s.session.store.IncrNextTargetMsgSeqNum())

		now := time.Now().UTC()
		s.session.SessionTime = internal.NewUTCTimeRange(
			internal.NewTimeOfDay(now.Add(time.Hour).Clock()),
			internal.NewTimeOfDay(now.Add(time.Duration(2)*time.Hour).Clock()),
		)

		if test.expectOnLogout {
			s.MockApp.On("OnLogout")
		}
		if test.expectSendLogout {
			s.MockApp.On("ToAdmin")
		}
		s.session.CheckSessionTime(s.session, now)

		s.MockApp.AssertExpectations(s.T())
		s.State(notSessionTime{})

		s.NextTargetMsgSeqNum(2)
		if test.expectSendLogout {
			s.LastToAdminMessageSent()
			s.MessageType(enum.MsgType_LOGOUT, s.MockApp.lastToAdmin)
			s.NextSenderMsgSeqNum(3)
		} else {
			s.NextSenderMsgSeqNum(2)
		}
	}
}
Example #3
0
func (s *SessionSuite) TestCheckSessionTimeInRange() {
	var tests = []struct {
		before, after sessionState
		expectReset   bool
	}{
		{before: latentState{}},
		{before: logonState{}},
		{before: logoutState{}},
		{before: inSession{}},
		{before: resendState{}},
		{before: pendingTimeout{resendState{}}},
		{before: pendingTimeout{inSession{}}},
		{before: notSessionTime{}, after: latentState{}, expectReset: true},
	}

	for _, test := range tests {
		s.SetupTest()
		s.session.State = test.before

		now := time.Now().UTC()
		store := new(memoryStore)
		if test.before.IsSessionTime() {
			store.Reset()
		} else {
			store.creationTime = now.Add(time.Duration(-1) * time.Minute)
		}
		s.session.store = store
		s.Nil(s.session.store.IncrNextSenderMsgSeqNum())
		s.Nil(s.session.store.IncrNextTargetMsgSeqNum())

		s.session.SessionTime = internal.NewUTCTimeRange(
			internal.NewTimeOfDay(now.Clock()),
			internal.NewTimeOfDay(now.Add(time.Hour).Clock()),
		)

		s.session.CheckSessionTime(s.session, now)
		if test.after != nil {
			s.State(test.after)
		} else {
			s.State(test.before)
		}

		if test.expectReset {
			s.ExpectStoreReset()
		} else {
			s.NextSenderMsgSeqNum(2)
			s.NextSenderMsgSeqNum(2)
		}
	}
}
Example #4
0
func (s *SessionSuite) TestTimeoutNotInSessionTime() {
	var tests = []struct {
		before           sessionState
		initiateLogon    bool
		expectOnLogout   bool
		expectSendLogout bool
	}{
		{before: logonState{}},
		{before: logonState{}, initiateLogon: true, expectOnLogout: true},
		{before: logoutState{}, expectOnLogout: true},
		{before: inSession{}, expectOnLogout: true, expectSendLogout: true},
		{before: resendState{}, expectOnLogout: true, expectSendLogout: true},
		{before: pendingTimeout{resendState{}}, expectOnLogout: true, expectSendLogout: true},
		{before: pendingTimeout{inSession{}}, expectOnLogout: true, expectSendLogout: true},
	}

	var events = []internal.Event{internal.PeerTimeout, internal.NeedHeartbeat, internal.LogonTimeout, internal.LogoutTimeout}

	for _, test := range tests {
		for _, event := range events {
			s.SetupTest()

			s.session.State = test.before
			s.session.InitiateLogon = test.initiateLogon
			s.Nil(s.session.store.IncrNextSenderMsgSeqNum())
			s.Nil(s.session.store.IncrNextTargetMsgSeqNum())

			now := time.Now().UTC()
			s.session.SessionTime = internal.NewUTCTimeRange(
				internal.NewTimeOfDay(now.Add(time.Hour).Clock()),
				internal.NewTimeOfDay(now.Add(time.Duration(2)*time.Hour).Clock()),
			)
			if test.expectOnLogout {
				s.MockApp.On("OnLogout")
			}
			if test.expectSendLogout {
				s.MockApp.On("ToAdmin")
			}

			s.session.Timeout(s.session, event)
			s.MockApp.AssertExpectations(s.T())
			s.State(notSessionTime{})
		}
	}
}
Example #5
0
func (s *SessionSuite) TestSendAppMessagesNotInSessionTime() {
	var tests = []struct {
		before           sessionState
		initiateLogon    bool
		expectOnLogout   bool
		expectSendLogout bool
	}{
		{before: logonState{}},
		{before: logonState{}, initiateLogon: true, expectOnLogout: true},
		{before: logoutState{}, expectOnLogout: true},
		{before: inSession{}, expectOnLogout: true, expectSendLogout: true},
		{before: resendState{}, expectOnLogout: true, expectSendLogout: true},
		{before: pendingTimeout{resendState{}}, expectOnLogout: true, expectSendLogout: true},
		{before: pendingTimeout{inSession{}}, expectOnLogout: true, expectSendLogout: true},
	}

	for _, test := range tests {
		s.SetupTest()

		s.session.State = test.before
		s.session.InitiateLogon = test.initiateLogon
		s.Nil(s.session.store.IncrNextSenderMsgSeqNum())
		s.Nil(s.session.store.IncrNextTargetMsgSeqNum())

		s.MockApp.On("ToApp").Return(nil)
		s.queueForSend(s.NewOrderSingle())
		s.MockApp.AssertExpectations(s.T())

		now := time.Now().UTC()
		s.session.SessionTime = internal.NewUTCTimeRange(
			internal.NewTimeOfDay(now.Add(time.Hour).Clock()),
			internal.NewTimeOfDay(now.Add(time.Duration(2)*time.Hour).Clock()),
		)
		if test.expectOnLogout {
			s.MockApp.On("OnLogout")
		}
		if test.expectSendLogout {
			s.MockApp.On("ToAdmin")
		}

		s.session.SendAppMessages(s.session)
		s.MockApp.AssertExpectations(s.T())
		s.State(notSessionTime{})
	}
}
Example #6
0
func (s *SessionSuite) TestIncomingNotInSessionTime() {
	var tests = []struct {
		before           sessionState
		initiateLogon    bool
		expectOnLogout   bool
		expectSendLogout bool
	}{
		{before: logonState{}},
		{before: logonState{}, initiateLogon: true, expectOnLogout: true},
		{before: logoutState{}, expectOnLogout: true},
		{before: inSession{}, expectOnLogout: true, expectSendLogout: true},
		{before: resendState{}, expectOnLogout: true, expectSendLogout: true},
		{before: pendingTimeout{resendState{}}, expectOnLogout: true, expectSendLogout: true},
		{before: pendingTimeout{inSession{}}, expectOnLogout: true, expectSendLogout: true},
	}

	for _, test := range tests {
		s.SetupTest()

		s.session.State = test.before
		s.session.InitiateLogon = test.initiateLogon
		s.IncrNextSenderMsgSeqNum()
		s.IncrNextTargetMsgSeqNum()

		now := time.Now().UTC()
		s.session.SessionTime = internal.NewUTCTimeRange(
			internal.NewTimeOfDay(now.Add(time.Hour).Clock()),
			internal.NewTimeOfDay(now.Add(time.Duration(2)*time.Hour).Clock()),
		)
		if test.expectOnLogout {
			s.MockApp.On("OnLogout")
		}
		if test.expectSendLogout {
			s.MockApp.On("ToAdmin")
		}

		msg := s.NewOrderSingle()
		msgBytes := msg.build()

		s.session.Incoming(s.session, fixIn{bytes: bytes.NewBuffer(msgBytes)})
		s.MockApp.AssertExpectations(s.T())
		s.State(notSessionTime{})
	}
}