gorepost/irc/irc_test.go

154 lines
3.1 KiB
Go
Raw Permalink Normal View History

// +build go1.4
2015-11-19 00:40:15 +00:00
// Copyright 2015 Robert S. Gerus. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package irc
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"sync"
"testing"
"time"
2015-12-14 14:12:18 +00:00
"github.com/arachnist/dyncfg"
2015-11-19 00:40:15 +00:00
)
var expectedOutput = []Message{
{
Command: "NICK",
Trailing: "gorepost",
},
{
Command: "USER",
Params: []string{"repost", "0", "*"},
Trailing: "https://github.com/arachnist/gorepost",
},
}
var input = []Message{
{
Command: "001",
Params: []string{"gorepost"},
},
}
var actualOutput []Message
var actualInput []Message
func fakeServer(t *testing.T) {
ln, err := net.Listen("tcp", ":36667")
if err != nil {
t.Error("fakeServer can't start listening")
}
// twice, to test reconnects
for range []int{0, 2} {
var wg sync.WaitGroup
conn, err := ln.Accept()
if err != nil {
t.Error("error accepting connection")
}
wg.Add(len(input))
2015-11-19 00:40:15 +00:00
// writer
go func(c net.Conn) {
writer := bufio.NewWriter(c)
for _, msg := range input {
writer.WriteString(msg.String() + endline)
writer.Flush()
wg.Done()
}
}(conn)
2015-12-09 00:58:11 +00:00
wg.Add(len(expectedOutput))
2015-11-19 00:40:15 +00:00
// reader
go func(c net.Conn) {
reader := bufio.NewReader(c)
for range expectedOutput {
raw, err := reader.ReadString(delim)
if err != nil {
t.Log("Failed reading message from client:", err)
t.Fail()
}
msg, err := ParseMessage(raw)
if err != nil {
t.Log("Failed parsing message from client:", raw)
t.Fail()
}
setupMutex.Lock()
2015-11-19 00:40:15 +00:00
actualOutput = append(actualOutput, *msg)
setupMutex.Unlock()
2015-11-19 00:40:15 +00:00
wg.Done()
}
}(conn)
wg.Wait()
time.Sleep(1 * time.Second)
conn.Close()
}
}
2015-12-09 00:58:11 +00:00
var setupMutex sync.Mutex
2015-11-19 00:40:15 +00:00
func TestSetup(t *testing.T) {
go fakeServer(t)
var conn Connection
2015-12-14 14:12:18 +00:00
conn.Setup(fakeDispatcher, "TestNet", dyncfg.New(configLookupHelper))
2015-11-19 00:40:15 +00:00
time.Sleep(2 * time.Second)
conn.Quit <- struct{}{}
// since we tested a reconnect, we should expect actual results to be
// multipled
2015-12-09 00:58:11 +00:00
setupMutex.Lock()
2015-11-19 00:40:15 +00:00
actualExpectedOutput := append(expectedOutput, expectedOutput...)
actualExpectedInput := append(input, input...)
2015-12-09 00:58:11 +00:00
setupMutex.Unlock()
2015-11-19 00:40:15 +00:00
2015-12-09 00:58:11 +00:00
setupMutex.Lock()
defer setupMutex.Unlock()
2015-11-19 00:40:15 +00:00
if fmt.Sprintf("%+v", actualExpectedOutput) != fmt.Sprintf("%+v", actualOutput) {
t.Log("Expected output does not match actual output")
t.Logf("expected: %+v\n", actualExpectedOutput)
t.Logf("actual : %+v\n", actualOutput)
t.Fail()
}
if fmt.Sprintf("%+v", actualExpectedInput) != fmt.Sprintf("%+v", actualInput) {
t.Log("Expected input does not match actual input")
t.Logf("expected: %+v\n", actualExpectedInput)
t.Logf("actual : %+v\n", actualInput)
t.Fail()
}
}
2015-11-29 15:29:30 +00:00
func fakeDispatcher(output func(Message), input Message) {
2015-11-19 00:40:15 +00:00
// nullify Context as it isn't transmitted over the wire
2015-12-09 00:58:11 +00:00
setupMutex.Lock()
defer setupMutex.Unlock()
2015-11-19 00:40:15 +00:00
input.Context = make(map[string]string)
actualInput = append(actualInput, input)
}
func configLookupHelper(map[string]string) []string {
return []string{".testconfig.json"}
}
func TestMain(m *testing.M) {
log.SetOutput(ioutil.Discard)
os.Exit(m.Run())
}