You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

eventsource_test.go 2.3KB

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