discord_test.go 4.6 KB

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