func TestClientAndStateTracking(t *testing.T) { ctrl := gomock.NewController(t) st := state.NewMockTracker(ctrl) c := SimpleClient("test", "test", "Testing IRC") // Assert some basic things about the initial state of the Conn struct if me := c.cfg.Me; me.Nick != "test" || me.Ident != "test" || me.Name != "Testing IRC" || me.Host != "" { t.Errorf("Conn.cfg.Me not correctly initialised.") } // Check that the internal handlers are correctly set up for k, _ := range intHandlers { if _, ok := c.handlers.set[strings.ToLower(k)]; !ok { t.Errorf("Missing internal handler for '%s'.", k) } } // Now enable the state tracking code and check its handlers c.EnableStateTracking() for k, _ := range stHandlers { if _, ok := c.handlers.set[strings.ToLower(k)]; !ok { t.Errorf("Missing state handler for '%s'.", k) } } if len(c.stRemovers) != len(stHandlers) { t.Errorf("Incorrect number of Removers (%d != %d) when adding state handlers.", len(c.stRemovers), len(stHandlers)) } // We're expecting the untracked me to be replaced by a tracked one if me := c.cfg.Me; me.Nick != "test" || me.Ident != "test" || me.Name != "Testing IRC" || me.Host != "" { t.Errorf("Enabling state tracking did not replace Me correctly.") } if c.st == nil || c.cfg.Me != c.st.Me() { t.Errorf("State tracker not enabled correctly.") } // Now, shim in the mock state tracker and test disabling state tracking me := c.cfg.Me c.st = st st.EXPECT().Wipe() c.DisableStateTracking() if c.st != nil || c.cfg.Me != me { t.Errorf("State tracker not disabled correctly.") } // Finally, check state tracking handlers were all removed correctly for k, _ := range stHandlers { if _, ok := c.handlers.set[strings.ToLower(k)]; ok && k != "NICK" { // A bit leaky, because intHandlers adds a NICK handler. t.Errorf("State handler for '%s' not removed correctly.", k) } } if len(c.stRemovers) != 0 { t.Errorf("stRemovers not zeroed correctly when removing state handlers.") } ctrl.Finish() }
// NOTE: including a second argument at all prevents calling c.postConnect() func setUp(t *testing.T, start ...bool) (*Conn, *testState) { ctrl := gomock.NewController(t) st := state.NewMockTracker(ctrl) nc := MockNetConn(t) c := SimpleClient("test", "test", "Testing IRC") c.st = st c.sock = nc c.cfg.Flood = true // Tests can take a while otherwise c.connected = true // If a second argument is passed to setUp, we tell postConnect not to // start the various goroutines that shuttle data around. c.postConnect(len(start) == 0) // Sleep 1ms to allow background routines to start. <-time.After(time.Millisecond) return c, &testState{ctrl, st, nc, c} }
// NOTE: including a second argument at all prevents calling c.postConnect() func setUp(t *testing.T, start ...bool) (*Conn, *testState) { ctrl := gomock.NewController(t) st := state.NewMockTracker(ctrl) nc := MockNetConn(t) c := SimpleClient("test", "test", "Testing IRC") logging.SetLogLevel(logging.LogFatal) c.st = st c.sock = nc c.cfg.Flood = true // Tests can take a while otherwise c.connected = true if len(start) == 0 { // Hack to allow tests of send, recv, write etc. // NOTE: the value of the boolean doesn't matter. c.postConnect() // Sleep 1ms to allow background routines to start. <-time.After(1e6) } return c, &testState{ctrl, st, nc, c} }