Mirror of github.com/gissleh/irc
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.4 KiB

7 years ago
  1. package irc_test
  2. import (
  3. "context"
  4. "math/rand"
  5. "strconv"
  6. "testing"
  7. "time"
  8. "git.aiterp.net/gisle/irc"
  9. )
  10. func TestHandle(t *testing.T) {
  11. rng := rand.NewSource(time.Now().UnixNano())
  12. eventName := strconv.FormatInt(rng.Int63(), 36) + strconv.FormatInt(rng.Int63(), 36) + strconv.FormatInt(rng.Int63(), 36)
  13. client := irc.New(context.Background(), irc.Config{})
  14. event := irc.NewEvent("test", eventName)
  15. handled := false
  16. handle := irc.Handle(func(event *irc.Event, client *irc.Client) {
  17. t.Log("Got:", event.Kind(), event.Verb())
  18. if event.Kind() == "test" && event.Verb() == eventName {
  19. handled = true
  20. }
  21. })
  22. client.EmitSync(context.Background(), event)
  23. if !handled {
  24. t.Error("Event wasn't handled")
  25. }
  26. if !irc.RemoveHandler(handle) {
  27. t.Error("Couldn't remove handler")
  28. }
  29. handled = false
  30. client.EmitSync(context.Background(), event)
  31. if handled {
  32. t.Error("Event was handled after handler was removed")
  33. }
  34. }
  35. func BenchmarkHandle(b *testing.B) {
  36. rng := rand.NewSource(time.Now().UnixNano())
  37. eventName := strconv.FormatInt(rng.Int63(), 36) + strconv.FormatInt(rng.Int63(), 36) + strconv.FormatInt(rng.Int63(), 36)
  38. client := irc.New(context.Background(), irc.Config{})
  39. event := irc.NewEvent("test", eventName)
  40. b.Run("Emit", func(b *testing.B) {
  41. for n := 0; n < b.N; n++ {
  42. client.Emit(event)
  43. }
  44. })
  45. b.Run("EmitSync", func(b *testing.B) {
  46. for n := 0; n < b.N; n++ {
  47. client.EmitSync(context.Background(), event)
  48. }
  49. })
  50. }