Example #1
0
func TestDispatch(t *testing.T) {

	dis := &Dispatcher{}
	l := line.Line{
		ChatBotId: 12,
		Command:   "PRIVMSG",
		Content:   "Hello",
		Channel:   "#foo"}

	queue := common.NewMockQueue()
	dis.queue = queue

	dis.Dispatch(&l)

	if len(queue.Got) != 1 {
		t.Error("Dispatch did not go on queue")
	}

	received, _ := queue.Got[QUEUE_PREFIX]
	if received == nil {
		t.Error("Expected '", QUEUE_PREFIX, "' as queue name")
	}
}
Example #2
0
// A serious integration test for BotBot.
// This covers BotBot, the IRC code, and the dispatcher.
func TestBotBotIRC(t *testing.T) {
	common.SetGlogFlags()

	// Create a mock storage with configuration in it
	storage := common.NewMockStorage(SERVER_PORT)

	// Create a mock queue to gather BotBot output
	queue := common.NewMockQueue()

	// Start a Mock IRC server, and gather writes to it
	server := common.NewMockIRCServer(TEST_MSG, SERVER_PORT)
	go server.Run(t)

	// Run BotBot
	time.Sleep(time.Second) // Sleep of one second to avoid the 5s backoff
	botbot := NewBotBot(storage, queue)
	go botbot.listen("testcmds")
	go botbot.mainLoop()
	waitForServer(server, 4)

	// this sleep allow us to keep the answer in the right order
	time.Sleep(time.Second)
	// Test sending a reply - should probably be separate test
	queue.ReadChannel <- "WRITE 1 #unit I am a plugin response"
	waitForServer(server, 6)

	tries := 0
	queue.RLock()
	q := queue.Got["q"]
	queue.RUnlock()

	for len(q) < 4 && tries < 4 {
		queue.RLock()
		q = queue.Got["q"]
		queue.RUnlock()

		glog.V(4).Infoln("[Debug] queue.Got[\"q\"]", len(q), "/", 4, q)
		time.Sleep(time.Second)
		tries++
	}
	checkContains(q, TEST_MSG, t)

	// Check IRC server expectations

	if server.GotLength() != 6 {
		t.Fatal("Expected exactly 6 IRC messages from the bot. Got ", server.GotLength())
	}

	glog.Infoln("[Debug] server.Got", server.Got)
	expect := []string{"PING", "USER", "NICK", "NickServ", "JOIN", "PRIVMSG"}
	for i := 0; i < 5; i++ {
		if !strings.Contains(string(server.Got[i]), expect[i]) {
			t.Error("Line ", i, " did not contain ", expect[i], ". It is: ", server.Got[i])
		}
	}

	// test shutdown - should probably be separate test

	botbot.shutdown()

	tries = 0
	val := 5
	for len(q) < val && tries < val {
		queue.RLock()
		q = queue.Got["q"]
		queue.RUnlock()
		glog.V(4).Infoln("[Debug] queue.Got[\"q\"]", len(q), "/", val, q)
		time.Sleep(time.Second)
		tries++
	}

	queue.RLock()
	checkContains(queue.Got["q"], "SHUTDOWN", t)
	queue.RUnlock()
}