This repository has been archived by the owner on Aug 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_test.go
80 lines (71 loc) · 1.78 KB
/
message_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package boxcat
import (
"fmt"
"log"
"testing"
"time"
"github.com/horgh/irc"
)
// Test one client sending a message to another client.
func TestPRIVMSG(t *testing.T) {
catbox, err := harnessCatbox("irc.example.org")
if err != nil {
t.Fatalf("error harnessing catbox: %s", err)
}
defer catbox.stop()
client1 := NewClient("client1", "127.0.0.1", catbox.Port)
recvChan1, sendChan1, _, err := client1.Start()
if err != nil {
t.Fatalf("error starting client: %s", err)
}
defer client1.Stop()
client2 := NewClient("client2", "127.0.0.1", catbox.Port)
recvChan2, _, _, err := client2.Start()
if err != nil {
t.Fatalf("error starting client: %s", err)
}
defer client2.Stop()
if waitForMessage(t, recvChan1, irc.Message{Command: irc.ReplyWelcome},
"welcome from %s", client1.GetNick()) == nil {
t.Fatalf("client1 did not get welcome")
}
if waitForMessage(t, recvChan2, irc.Message{Command: irc.ReplyWelcome},
"welcome from %s", client2.GetNick()) == nil {
t.Fatalf("client2 did not get welcome")
}
sendChan1 <- irc.Message{
Command: "PRIVMSG",
Params: []string{client2.GetNick(), "hi there"},
}
if waitForMessage(
t,
recvChan2,
irc.Message{
Command: "PRIVMSG",
Params: []string{client2.GetNick(), "hi there"},
},
"%s received PRIVMSG from %s", client1.GetNick(), client2.GetNick(),
) == nil {
t.Fatalf("client1 did not receive message from client2")
}
}
func waitForMessage(
t *testing.T,
ch <-chan irc.Message,
want irc.Message,
format string,
a ...interface{},
) *irc.Message {
for {
select {
case <-time.After(10 * time.Second):
t.Logf("timeout waiting for message: %s", want)
return nil
case got := <-ch:
if got.Command == want.Command {
log.Printf("got command: %s", fmt.Sprintf(format, a...))
return &got
}
}
}
}