Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

eventsource_test.go 2.4KB

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