discord_test.go 4.5 KB

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