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.

65 lines
1.4 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 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", eventName)
  35. wg := sync.WaitGroup{}
  36. client.AddHandler(func(event2 *irc.Event, client *irc.Client) {
  37. wg.Done()
  38. })
  39. b.Run("Emit", func(b *testing.B) {
  40. wg.Add(b.N)
  41. for n := 0; n < b.N; n++ {
  42. client.Emit(event)
  43. }
  44. wg.Wait()
  45. })
  46. b.Run("EmitSync", func(b *testing.B) {
  47. wg.Add(b.N)
  48. for n := 0; n < b.N; n++ {
  49. client.EmitSync(context.Background(), event)
  50. }
  51. wg.Wait()
  52. })
  53. }