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.

60 lines
1.4 KiB

7 years ago
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. 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. handled = false
  27. client.EmitSync(context.Background(), event)
  28. if handled {
  29. t.Error("Event was handled after handler was removed")
  30. }
  31. }
  32. func BenchmarkHandle(b *testing.B) {
  33. rng := rand.NewSource(time.Now().UnixNano())
  34. eventName := strconv.FormatInt(rng.Int63(), 36) + strconv.FormatInt(rng.Int63(), 36) + strconv.FormatInt(rng.Int63(), 36)
  35. client := irc.New(context.Background(), irc.Config{})
  36. event := irc.NewEvent("test", eventName)
  37. b.Run("Emit", func(b *testing.B) {
  38. for n := 0; n < b.N; n++ {
  39. client.Emit(event)
  40. }
  41. })
  42. b.Run("EmitSync", func(b *testing.B) {
  43. for n := 0; n < b.N; n++ {
  44. client.EmitSync(context.Background(), event)
  45. }
  46. })
  47. }