discord_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. // TestNew tests the New() function without any arguments. This should return
  37. // a valid Session{} struct and no errors.
  38. func TestNew(t *testing.T) {
  39. _, err := New()
  40. if err != nil {
  41. t.Errorf("New() returned error: %+v", err)
  42. }
  43. }
  44. // TestInvalidToken tests the New() function with an invalid token
  45. func TestInvalidToken(t *testing.T) {
  46. d, err := New("asjkldhflkjasdh")
  47. if err != nil {
  48. t.Fatalf("New(InvalidToken) returned error: %+v", err)
  49. }
  50. // New with just a token does not do any communication, so attempt an api call.
  51. _, err = d.UserSettings()
  52. if err == nil {
  53. t.Errorf("New(InvalidToken), d.UserSettings returned nil error.")
  54. }
  55. }
  56. // TestNewToken tests the New() function with a Token.
  57. func TestNewToken(t *testing.T) {
  58. if envToken == "" {
  59. t.Skip("Skipping New(token), DGU_TOKEN not set")
  60. }
  61. d, err := New(envToken)
  62. if err != nil {
  63. t.Fatalf("New(envToken) returned error: %+v", err)
  64. }
  65. if d == nil {
  66. t.Fatal("New(envToken), d is nil, should be Session{}")
  67. }
  68. if d.Token == "" {
  69. t.Fatal("New(envToken), d.Token is empty, should be a valid Token.")
  70. }
  71. }
  72. func TestOpenClose(t *testing.T) {
  73. if envToken == "" {
  74. t.Skip("Skipping TestClose, DGU_TOKEN not set")
  75. }
  76. d, err := New(envToken)
  77. if err != nil {
  78. t.Fatalf("TestClose, New(envToken) returned error: %+v", err)
  79. }
  80. if err = d.Open(); err != nil {
  81. t.Fatalf("TestClose, d.Open failed: %+v", err)
  82. }
  83. // We need a better way to know the session is ready for use,
  84. // this is totally gross.
  85. start := time.Now()
  86. for {
  87. d.RLock()
  88. if d.DataReady {
  89. d.RUnlock()
  90. break
  91. }
  92. d.RUnlock()
  93. if time.Since(start) > 10*time.Second {
  94. t.Fatal("DataReady never became true.yy")
  95. }
  96. runtime.Gosched()
  97. }
  98. // TODO find a better way
  99. // Add a small sleep here to make sure heartbeat and other events
  100. // have enough time to get fired. Need a way to actually check
  101. // those events.
  102. time.Sleep(2 * time.Second)
  103. // UpdateStatus - maybe we move this into wsapi_test.go but the websocket
  104. // created here is needed. This helps tests that the websocket was setup
  105. // and it is working.
  106. if err = d.UpdateStatus(0, time.Now().String()); err != nil {
  107. t.Errorf("UpdateStatus error: %+v", err)
  108. }
  109. if err = d.Close(); err != nil {
  110. t.Fatalf("TestClose, d.Close failed: %+v", err)
  111. }
  112. }
  113. func TestAddHandler(t *testing.T) {
  114. testHandlerCalled := int32(0)
  115. testHandler := func(s *Session, m *MessageCreate) {
  116. atomic.AddInt32(&testHandlerCalled, 1)
  117. }
  118. interfaceHandlerCalled := int32(0)
  119. interfaceHandler := func(s *Session, i interface{}) {
  120. atomic.AddInt32(&interfaceHandlerCalled, 1)
  121. }
  122. bogusHandlerCalled := int32(0)
  123. bogusHandler := func(s *Session, se *Session) {
  124. atomic.AddInt32(&bogusHandlerCalled, 1)
  125. }
  126. d := Session{}
  127. d.AddHandler(testHandler)
  128. d.AddHandler(testHandler)
  129. d.AddHandler(interfaceHandler)
  130. d.AddHandler(bogusHandler)
  131. d.handleEvent(messageCreateEventType, &MessageCreate{})
  132. d.handleEvent(messageDeleteEventType, &MessageDelete{})
  133. <-time.After(500 * time.Millisecond)
  134. // testHandler will be called twice because it was added twice.
  135. if atomic.LoadInt32(&testHandlerCalled) != 2 {
  136. t.Fatalf("testHandler was not called twice.")
  137. }
  138. // interfaceHandler will be called twice, once for each event.
  139. if atomic.LoadInt32(&interfaceHandlerCalled) != 2 {
  140. t.Fatalf("interfaceHandler was not called twice.")
  141. }
  142. if atomic.LoadInt32(&bogusHandlerCalled) != 0 {
  143. t.Fatalf("bogusHandler was called.")
  144. }
  145. }
  146. func TestRemoveHandler(t *testing.T) {
  147. testHandlerCalled := int32(0)
  148. testHandler := func(s *Session, m *MessageCreate) {
  149. atomic.AddInt32(&testHandlerCalled, 1)
  150. }
  151. d := Session{}
  152. r := d.AddHandler(testHandler)
  153. d.handleEvent(messageCreateEventType, &MessageCreate{})
  154. r()
  155. d.handleEvent(messageCreateEventType, &MessageCreate{})
  156. <-time.After(500 * time.Millisecond)
  157. // testHandler will be called once, as it was removed in between calls.
  158. if atomic.LoadInt32(&testHandlerCalled) != 1 {
  159. t.Fatalf("testHandler was not called once.")
  160. }
  161. }