Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

eventsource_test.go 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package integrations
  5. import (
  6. "fmt"
  7. "net/http"
  8. "testing"
  9. "time"
  10. "code.gitea.io/gitea/models"
  11. repo_model "code.gitea.io/gitea/models/repo"
  12. "code.gitea.io/gitea/models/unittest"
  13. user_model "code.gitea.io/gitea/models/user"
  14. "code.gitea.io/gitea/modules/eventsource"
  15. api "code.gitea.io/gitea/modules/structs"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. func TestEventSourceManagerRun(t *testing.T) {
  19. defer prepareTestEnv(t)()
  20. manager := eventsource.GetManager()
  21. eventChan := manager.Register(2)
  22. defer func() {
  23. manager.Unregister(2, eventChan)
  24. // ensure the eventChan is closed
  25. for {
  26. _, ok := <-eventChan
  27. if !ok {
  28. break
  29. }
  30. }
  31. }()
  32. expectNotificationCountEvent := func(count int64) func() bool {
  33. return func() bool {
  34. select {
  35. case event, ok := <-eventChan:
  36. if !ok {
  37. return false
  38. }
  39. data, ok := event.Data.(models.UserIDCount)
  40. if !ok {
  41. return false
  42. }
  43. return event.Name == "notification-count" && data.Count == count
  44. default:
  45. return false
  46. }
  47. }
  48. }
  49. user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
  50. repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
  51. thread5 := unittest.AssertExistsAndLoadBean(t, &models.Notification{ID: 5}).(*models.Notification)
  52. assert.NoError(t, thread5.LoadAttributes())
  53. session := loginUser(t, user2.Name)
  54. token := getTokenForLoggedInUser(t, session)
  55. var apiNL []api.NotificationThread
  56. // -- mark notifications as read --
  57. req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?status-types=unread&token=%s", token))
  58. resp := session.MakeRequest(t, req, http.StatusOK)
  59. DecodeJSON(t, resp, &apiNL)
  60. assert.Len(t, apiNL, 2)
  61. lastReadAt := "2000-01-01T00%3A50%3A01%2B00%3A00" // 946687801 <- only Notification 4 is in this filter ...
  62. req = NewRequest(t, "PUT", fmt.Sprintf("/api/v1/repos/%s/%s/notifications?last_read_at=%s&token=%s", user2.Name, repo1.Name, lastReadAt, token))
  63. session.MakeRequest(t, req, http.StatusResetContent)
  64. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?token=%s&status-types=unread", token))
  65. resp = session.MakeRequest(t, req, http.StatusOK)
  66. DecodeJSON(t, resp, &apiNL)
  67. assert.Len(t, apiNL, 1)
  68. assert.Eventually(t, expectNotificationCountEvent(1), 30*time.Second, 1*time.Second)
  69. }