discord_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package discordgo
  2. import (
  3. "fmt"
  4. "os"
  5. "runtime"
  6. "sync/atomic"
  7. "testing"
  8. "time"
  9. )
  10. //////////////////////////////////////////////////////////////////////////////
  11. ////////////////////////////////////////////////////// VARS NEEDED FOR TESTING
  12. var (
  13. dg *Session // Stores a global discordgo user session
  14. dgBot *Session // Stores a global discordgo bot session
  15. envToken = os.Getenv("DGU_TOKEN") // Token to use when authenticating the user account
  16. envBotToken = os.Getenv("DGB_TOKEN") // Token to use when authenticating the bot account
  17. envGuild = os.Getenv("DG_GUILD") // Guild ID to use for tests
  18. envChannel = os.Getenv("DG_CHANNEL") // Channel ID to use for tests
  19. envAdmin = os.Getenv("DG_ADMIN") // User ID of admin user to use for tests
  20. )
  21. func init() {
  22. fmt.Println("Init is being called.")
  23. if envBotToken != "" {
  24. if d, err := New(envBotToken); err == nil {
  25. dgBot = d
  26. }
  27. }
  28. if d, err := New(envToken); err == nil {
  29. dg = d
  30. } else {
  31. fmt.Println("dg is nil, error", err)
  32. }
  33. }
  34. //////////////////////////////////////////////////////////////////////////////
  35. /////////////////////////////////////////////////////////////// START OF TESTS
  36. // TestNewToken tests the New() function with a Token.
  37. func TestNewToken(t *testing.T) {
  38. if envToken == "" {
  39. t.Skip("Skipping New(token), DGU_TOKEN not set")
  40. }
  41. d, err := New(envToken)
  42. if err != nil {
  43. t.Fatalf("New(envToken) returned error: %+v", err)
  44. }
  45. if d == nil {
  46. t.Fatal("New(envToken), d is nil, should be Session{}")
  47. }
  48. if d.Token == "" {
  49. t.Fatal("New(envToken), d.Token is empty, should be a valid Token.")
  50. }
  51. }
  52. func TestOpenClose(t *testing.T) {
  53. if envToken == "" {
  54. t.Skip("Skipping TestClose, DGU_TOKEN not set")
  55. }
  56. d, err := New(envToken)
  57. if err != nil {
  58. t.Fatalf("TestClose, New(envToken) returned error: %+v", err)
  59. }
  60. if err = d.Open(); err != nil {
  61. t.Fatalf("TestClose, d.Open failed: %+v", err)
  62. }
  63. // We need a better way to know the session is ready for use,
  64. // this is totally gross.
  65. start := time.Now()
  66. for {
  67. d.RLock()
  68. if d.DataReady {
  69. d.RUnlock()
  70. break
  71. }
  72. d.RUnlock()
  73. if time.Since(start) > 10*time.Second {
  74. t.Fatal("DataReady never became true.yy")
  75. }
  76. runtime.Gosched()
  77. }
  78. // TODO find a better way
  79. // Add a small sleep here to make sure heartbeat and other events
  80. // have enough time to get fired. Need a way to actually check
  81. // those events.
  82. time.Sleep(2 * time.Second)
  83. // UpdateStatus - maybe we move this into wsapi_test.go but the websocket
  84. // created here is needed. This helps tests that the websocket was setup
  85. // and it is working.
  86. if err = d.UpdateGameStatus(0, time.Now().String()); err != nil {
  87. t.Errorf("UpdateStatus error: %+v", err)
  88. }
  89. if err = d.Close(); err != nil {
  90. t.Fatalf("TestClose, d.Close failed: %+v", err)
  91. }
  92. }
  93. func TestAddHandler(t *testing.T) {
  94. testHandlerCalled := int32(0)
  95. testHandler := func(s *Session, m *MessageCreate) {
  96. atomic.AddInt32(&testHandlerCalled, 1)
  97. }
  98. interfaceHandlerCalled := int32(0)
  99. interfaceHandler := func(s *Session, i interface{}) {
  100. atomic.AddInt32(&interfaceHandlerCalled, 1)
  101. }
  102. bogusHandlerCalled := int32(0)
  103. bogusHandler := func(s *Session, se *Session) {
  104. atomic.AddInt32(&bogusHandlerCalled, 1)
  105. }
  106. d := Session{}
  107. d.AddHandler(testHandler)
  108. d.AddHandler(testHandler)
  109. d.AddHandler(interfaceHandler)
  110. d.AddHandler(bogusHandler)
  111. d.handleEvent(messageCreateEventType, &MessageCreate{})
  112. d.handleEvent(messageDeleteEventType, &MessageDelete{})
  113. <-time.After(500 * time.Millisecond)
  114. // testHandler will be called twice because it was added twice.
  115. if atomic.LoadInt32(&testHandlerCalled) != 2 {
  116. t.Fatalf("testHandler was not called twice.")
  117. }
  118. // interfaceHandler will be called twice, once for each event.
  119. if atomic.LoadInt32(&interfaceHandlerCalled) != 2 {
  120. t.Fatalf("interfaceHandler was not called twice.")
  121. }
  122. if atomic.LoadInt32(&bogusHandlerCalled) != 0 {
  123. t.Fatalf("bogusHandler was called.")
  124. }
  125. }
  126. func TestRemoveHandler(t *testing.T) {
  127. testHandlerCalled := int32(0)
  128. testHandler := func(s *Session, m *MessageCreate) {
  129. atomic.AddInt32(&testHandlerCalled, 1)
  130. }
  131. d := Session{}
  132. r := d.AddHandler(testHandler)
  133. d.handleEvent(messageCreateEventType, &MessageCreate{})
  134. r()
  135. d.handleEvent(messageCreateEventType, &MessageCreate{})
  136. <-time.After(500 * time.Millisecond)
  137. // testHandler will be called once, as it was removed in between calls.
  138. if atomic.LoadInt32(&testHandlerCalled) != 1 {
  139. t.Fatalf("testHandler was not called once.")
  140. }
  141. }