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.

55 lines
1.2 KiB

7 years ago
7 years ago
7 years ago
  1. package irc_test
  2. import (
  3. "context"
  4. "math/rand"
  5. "strconv"
  6. "testing"
  7. "time"
  8. "github.com/gissleh/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. client.AddHandler(func(event *irc.Event, client *irc.Client) {
  17. if !handled {
  18. t.Log("Got:", event.Kind(), event.Verb())
  19. }
  20. if event.Kind() == "test" && event.Verb() == eventName {
  21. handled = true
  22. }
  23. })
  24. client.EmitSync(context.Background(), event)
  25. if !handled {
  26. t.Error("Event wasn't handled")
  27. }
  28. }
  29. func BenchmarkHandle(b *testing.B) {
  30. rng := rand.NewSource(time.Now().UnixNano())
  31. eventName := strconv.FormatInt(rng.Int63(), 36) + strconv.FormatInt(rng.Int63(), 36) + strconv.FormatInt(rng.Int63(), 36)
  32. client := irc.New(context.Background(), irc.Config{})
  33. event := irc.NewEvent("test", eventName)
  34. b.Run("Emit", func(b *testing.B) {
  35. for n := 0; n < b.N; n++ {
  36. client.Emit(event)
  37. }
  38. })
  39. b.Run("EmitSync", func(b *testing.B) {
  40. for n := 0; n < b.N; n++ {
  41. client.EmitSync(context.Background(), event)
  42. }
  43. })
  44. }