Added unique id to callbacks so they can be referenced. Since Go doens't actually provide unique function pointers, we use the closest we can get by grabbing the pointer for the function and slapping a random int on the end. Does it guarantee there will never be a collision? No, but it makes it's pretty damn unlikely that you'll get one during the lifetime of an app unless you are generating millions and millions of callbacks and never, ever deleting them, in which case you probably have something else to worry about
This commit is contained in:
50
irc_test.go
50
irc_test.go
@@ -44,3 +44,53 @@ func TestConnectionSSL(t *testing.T) {
|
||||
|
||||
irccon.Loop()
|
||||
}
|
||||
|
||||
func TestRemoveCallback(t *testing.T) {
|
||||
irccon := IRC("go-eventirc", "go-eventirc")
|
||||
irccon.VerboseCallbackHandler = true
|
||||
|
||||
done := make(chan int, 10)
|
||||
|
||||
irccon.AddCallback("TEST", func(e *Event) { done <- 1 })
|
||||
id := irccon.AddCallback("TEST", func(e *Event) { done <- 2 })
|
||||
irccon.AddCallback("TEST", func(e *Event) { done <- 3 })
|
||||
|
||||
// Should remove callback at index 1
|
||||
irccon.RemoveCallback("TEST", id)
|
||||
|
||||
irccon.RunCallbacks(&Event{
|
||||
Code: "TEST",
|
||||
})
|
||||
|
||||
var results []int
|
||||
|
||||
results = append(results, <-done)
|
||||
results = append(results, <-done)
|
||||
|
||||
if len(results) != 2 || !(results[0] == 1 && results[1] == 3) {
|
||||
t.Error("Callback 2 not removed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWildcardCallback(t *testing.T) {
|
||||
irccon := IRC("go-eventirc", "go-eventirc")
|
||||
irccon.VerboseCallbackHandler = true
|
||||
|
||||
done := make(chan int, 10)
|
||||
|
||||
irccon.AddCallback("TEST", func(e *Event) { done <- 1 })
|
||||
irccon.AddCallback("*", func(e *Event) { done <- 2 })
|
||||
|
||||
irccon.RunCallbacks(&Event{
|
||||
Code: "TEST",
|
||||
})
|
||||
|
||||
var results []int
|
||||
|
||||
results = append(results, <-done)
|
||||
results = append(results, <-done)
|
||||
|
||||
if len(results) != 2 || !(results[0] == 1 && results[1] == 2) {
|
||||
t.Error("Wildcard callback not called")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user