-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #182 from zneix/zneix/feat/handle-self-joins-parts
Add callbacks for self JOIN/PART messages
- Loading branch information
Showing
3 changed files
with
91 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package twitch | |
|
||
import ( | ||
"bufio" | ||
"context" | ||
"crypto/tls" | ||
"fmt" | ||
"net" | ||
|
@@ -838,36 +839,55 @@ func TestCanReceiveJOINMessage(t *testing.T) { | |
assertMessageTypesEqual(t, JOIN, received.GetType()) | ||
} | ||
|
||
func TestDoesNotReceiveJOINMessageFromSelf(t *testing.T) { | ||
func TestReceiveJOINMessageWithSelfJOIN(t *testing.T) { | ||
t.Parallel() | ||
testMessages := []string{ | ||
`:[email protected] JOIN #mychannel`, | ||
`:[email protected] JOIN #mychannel`, | ||
} | ||
|
||
wait := make(chan struct{}) | ||
var received UserJoinMessage | ||
var receivedOther UserJoinMessage | ||
var receivedSelf UserJoinMessage | ||
|
||
host := startServer(t, postMessagesOnConnect(testMessages), nothingOnMessage) | ||
client := newTestClient(host) | ||
|
||
wg := new(sync.WaitGroup) | ||
wg.Add(2) | ||
|
||
client.OnUserJoinMessage(func(message UserJoinMessage) { | ||
received = message | ||
close(wait) | ||
receivedOther = message | ||
wg.Done() | ||
}) | ||
|
||
client.OnSelfJoinMessage(func(message UserJoinMessage) { | ||
receivedSelf = message | ||
wg.Done() | ||
}) | ||
|
||
go client.Connect() | ||
|
||
// hack with ctx makes it possible to use it in select statement below | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
go func() { | ||
wg.Wait() | ||
cancel() | ||
}() | ||
|
||
// wait for server to start | ||
select { | ||
case <-wait: | ||
case <-ctx.Done(): | ||
case <-time.After(time.Second * 3): | ||
t.Fatal("no message sent") | ||
} | ||
|
||
assertStringsEqual(t, "username123", received.User) | ||
assertStringsEqual(t, "mychannel", received.Channel) | ||
assertMessageTypesEqual(t, JOIN, received.GetType()) | ||
assertStringsEqual(t, "username123", receivedOther.User) | ||
assertStringsEqual(t, "mychannel", receivedOther.Channel) | ||
assertMessageTypesEqual(t, JOIN, receivedOther.GetType()) | ||
|
||
assertStringsEqual(t, "justinfan123123", receivedSelf.User) | ||
assertStringsEqual(t, "mychannel", receivedSelf.Channel) | ||
assertMessageTypesEqual(t, JOIN, receivedSelf.GetType()) | ||
} | ||
|
||
func TestCanReceivePARTMessage(t *testing.T) { | ||
|
@@ -899,36 +919,55 @@ func TestCanReceivePARTMessage(t *testing.T) { | |
assertMessageTypesEqual(t, PART, received.GetType()) | ||
} | ||
|
||
func TestDoesNotReceivePARTMessageFromSelf(t *testing.T) { | ||
func TestReceivePARTMessageWithSelfPART(t *testing.T) { | ||
t.Parallel() | ||
testMessages := []string{ | ||
`:[email protected] PART #mychannel`, | ||
`:[email protected] PART #mychannel`, | ||
} | ||
|
||
wait := make(chan struct{}) | ||
var received UserPartMessage | ||
var receivedOther UserPartMessage | ||
var receivedSelf UserPartMessage | ||
|
||
host := startServer(t, postMessagesOnConnect(testMessages), nothingOnMessage) | ||
client := newTestClient(host) | ||
|
||
wg := new(sync.WaitGroup) | ||
wg.Add(2) | ||
|
||
client.OnUserPartMessage(func(message UserPartMessage) { | ||
received = message | ||
close(wait) | ||
receivedOther = message | ||
wg.Done() | ||
}) | ||
|
||
client.OnSelfPartMessage(func(message UserPartMessage) { | ||
receivedSelf = message | ||
wg.Done() | ||
}) | ||
|
||
go client.Connect() | ||
|
||
// hack with ctx makes it possible to use it in select statement below | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
go func() { | ||
wg.Wait() | ||
cancel() | ||
}() | ||
|
||
// wait for server to start | ||
select { | ||
case <-wait: | ||
case <-ctx.Done(): | ||
case <-time.After(time.Second * 3): | ||
t.Fatal("no message sent") | ||
} | ||
|
||
assertStringsEqual(t, "username123", received.User) | ||
assertStringsEqual(t, "mychannel", received.Channel) | ||
assertMessageTypesEqual(t, PART, received.GetType()) | ||
assertStringsEqual(t, "username123", receivedOther.User) | ||
assertStringsEqual(t, "mychannel", receivedOther.Channel) | ||
assertMessageTypesEqual(t, PART, receivedOther.GetType()) | ||
|
||
assertStringsEqual(t, "justinfan123123", receivedSelf.User) | ||
assertStringsEqual(t, "mychannel", receivedSelf.Channel) | ||
assertMessageTypesEqual(t, PART, receivedSelf.GetType()) | ||
} | ||
|
||
func TestCanReceiveUNSETMessage(t *testing.T) { | ||
|