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.

67 lines
1.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package irc_test
  2. import (
  3. "context"
  4. "math/rand"
  5. "strconv"
  6. "sync"
  7. "testing"
  8. "time"
  9. "github.com/gissleh/irc"
  10. )
  11. func TestHandle(t *testing.T) {
  12. rng := rand.NewSource(time.Now().UnixNano())
  13. eventName := strconv.FormatInt(rng.Int63(), 36) + strconv.FormatInt(rng.Int63(), 36) + strconv.FormatInt(rng.Int63(), 36)
  14. client := irc.New(context.Background(), irc.Config{})
  15. event := irc.NewEvent("test", eventName)
  16. handled := false
  17. client.AddHandler(func(event *irc.Event, client *irc.Client) {
  18. if !handled {
  19. t.Log("Got:", event.Kind(), event.Verb())
  20. }
  21. if event.Kind() == "test" && event.Verb() == eventName {
  22. handled = true
  23. }
  24. })
  25. client.EmitSync(context.Background(), event)
  26. if !handled {
  27. t.Error("Event wasn't handled")
  28. }
  29. }
  30. func BenchmarkHandle(b *testing.B) {
  31. rng := rand.NewSource(time.Now().UnixNano())
  32. eventName := strconv.FormatInt(rng.Int63(), 36) + strconv.FormatInt(rng.Int63(), 36) + strconv.FormatInt(rng.Int63(), 36)
  33. client := irc.New(context.Background(), irc.Config{})
  34. event := irc.NewEvent("test_event", eventName)
  35. wg := sync.WaitGroup{}
  36. client.AddHandler(func(event2 *irc.Event, _ *irc.Client) {
  37. if event2.Kind() == "test_event" {
  38. wg.Done()
  39. }
  40. })
  41. b.Run("Emit", func(b *testing.B) {
  42. wg.Add(b.N)
  43. for n := 0; n < b.N; n++ {
  44. client.Emit(event)
  45. }
  46. wg.Wait()
  47. })
  48. b.Run("EmitSync", func(b *testing.B) {
  49. wg.Add(b.N)
  50. for n := 0; n < b.N; n++ {
  51. client.EmitSync(context.Background(), event)
  52. }
  53. wg.Wait()
  54. })
  55. }