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.

53 lines
1.2 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. }
  27. func BenchmarkHandle(b *testing.B) {
  28. rng := rand.NewSource(time.Now().UnixNano())
  29. eventName := strconv.FormatInt(rng.Int63(), 36) + strconv.FormatInt(rng.Int63(), 36) + strconv.FormatInt(rng.Int63(), 36)
  30. client := irc.New(context.Background(), irc.Config{})
  31. event := irc.NewEvent("test", eventName)
  32. b.Run("Emit", func(b *testing.B) {
  33. for n := 0; n < b.N; n++ {
  34. client.Emit(event)
  35. }
  36. })
  37. b.Run("EmitSync", func(b *testing.B) {
  38. for n := 0; n < b.N; n++ {
  39. client.EmitSync(context.Background(), event)
  40. }
  41. })
  42. }