Skip to content

Commit 7e99301

Browse files
committed
Improve pull_merge_test
1 parent d313a4e commit 7e99301

File tree

2 files changed

+40
-54
lines changed

2 files changed

+40
-54
lines changed

integrations/notification_helper_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,13 @@ func (n *NotifierListener) Register(functionName string, callback *func(string,
4747
// Deregister will remove the provided callback from the provided notifier function
4848
func (n *NotifierListener) Deregister(functionName string, callback *func(string, [][]byte)) {
4949
n.lock.Lock()
50-
found := -1
50+
defer n.lock.Unlock()
5151
for i, callbackPtr := range n.callbacks[functionName] {
5252
if callbackPtr == callback {
53-
found = i
54-
break
53+
n.callbacks[functionName] = append(n.callbacks[functionName][0:i], n.callbacks[functionName][i+1:]...)
54+
return
5555
}
5656
}
57-
if found > -1 {
58-
n.callbacks[functionName] = append(n.callbacks[functionName][0:found], n.callbacks[functionName][found+1:]...)
59-
}
60-
n.lock.Unlock()
6157
}
6258

6359
// RegisterChannel will return a registered channel with function name and return a function to deregister it and close the channel at the end

integrations/pull_merge_test.go

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -59,70 +59,60 @@ func testPullCleanUp(t *testing.T, session *TestSession, user, repo, pullnum str
5959
return resp
6060
}
6161

62+
func checkChannelWithTimeout(c <-chan interface{}, timeout time.Duration, callback func(interface{}) bool) bool {
63+
timer := time.NewTimer(500 * time.Millisecond)
64+
for {
65+
select {
66+
case received := <-c:
67+
if callback(received) {
68+
if !timer.Stop() {
69+
select {
70+
case <-timer.C:
71+
default:
72+
}
73+
}
74+
return true
75+
}
76+
case <-timer.C:
77+
return false
78+
}
79+
}
80+
}
81+
6282
func TestPullMerge(t *testing.T) {
6383
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
64-
createPullNotified, deferableCreate := notifierListener.RegisterChannel("NotifyNewPullRequest", 0, &models.PullRequest{})
65-
defer deferableCreate()
84+
createPullNotified, unregisterNewPull := notifierListener.RegisterChannel("NotifyNewPullRequest", 0, &models.PullRequest{})
85+
defer unregisterNewPull()
6686

67-
mergePullNotified, deferableMerge := notifierListener.RegisterChannel("NotifyMergePullRequest", 0, &models.PullRequest{})
68-
defer deferableMerge()
87+
mergePullNotified, unregisterMergePull := notifierListener.RegisterChannel("NotifyMergePullRequest", 0, &models.PullRequest{})
88+
defer unregisterMergePull()
6989

7090
session := loginUser(t, "user1")
7191
testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
7292
testEditFile(t, session, "user1", "repo1", "master", "README.md", "Hello, World (Edited)\n")
7393

74-
var prInterface interface{}
75-
7694
resp := testPullCreate(t, session, "user1", "repo1", "master", "This is a pull title")
77-
select {
78-
case prInterface = <-createPullNotified:
79-
case <-time.After(500 * time.Millisecond):
80-
assert.Fail(t, "Took too long to notify!")
95+
96+
isOurPR := func(received interface{}) bool {
97+
pr := received.(*models.PullRequest)
98+
pr.LoadBaseRepo()
99+
pr.LoadHeadRepo()
100+
return pr.BaseRepo.FullName() == "user2/repo1" &&
101+
pr.BaseBranch == "master" &&
102+
pr.HeadRepo.FullName() == "user1/repo1" &&
103+
pr.HeadBranch == "master"
81104
}
82-
pr := prInterface.(*models.PullRequest)
83-
pr.LoadBaseRepo()
84-
pr.LoadHeadRepo()
85-
pr.BaseRepo.MustOwner()
86-
pr.HeadRepo.MustOwner()
87105

88-
assert.EqualValues(t, "user1", pr.HeadRepo.Owner.Name)
89-
assert.EqualValues(t, "repo1", pr.HeadRepo.Name)
90-
assert.EqualValues(t, "user2", pr.BaseRepo.Owner.Name)
91-
assert.EqualValues(t, "repo1", pr.BaseRepo.Name)
106+
assert.True(t, checkChannelWithTimeout(createPullNotified, 500*time.Millisecond, isOurPR), "Failed to be notified pull created")
92107

93108
elem := strings.Split(test.RedirectURL(resp), "/")
94109
assert.EqualValues(t, "pulls", elem[3])
95110

96111
testPullMerge(t, session, elem[1], elem[2], elem[4], models.MergeStyleMerge)
112+
assert.True(t, checkChannelWithTimeout(mergePullNotified, 500*time.Millisecond, isOurPR), "Failed to be notified pull merged")
97113

98-
select {
99-
case prInterface = <-mergePullNotified:
100-
case <-time.After(500 * time.Millisecond):
101-
assert.Fail(t, "Took too long to notify!")
102-
}
103-
104-
pr = prInterface.(*models.PullRequest)
105-
pr.LoadBaseRepo()
106-
pr.LoadHeadRepo()
107-
pr.BaseRepo.MustOwner()
108-
pr.HeadRepo.MustOwner()
109-
110-
assert.EqualValues(t, "user1", pr.HeadRepo.Owner.Name)
111-
assert.EqualValues(t, "repo1", pr.HeadRepo.Name)
112-
assert.EqualValues(t, "user2", pr.BaseRepo.Owner.Name)
113-
assert.EqualValues(t, "repo1", pr.BaseRepo.Name)
114-
115-
time.Sleep(100 * time.Millisecond)
116-
select {
117-
case prInterface = <-createPullNotified:
118-
assert.Fail(t, "Should only have one pull create notification: %v", prInterface)
119-
default:
120-
}
121-
select {
122-
case prInterface = <-mergePullNotified:
123-
assert.Fail(t, "Should only have one pull merge notification: %v", prInterface)
124-
default:
125-
}
114+
assert.False(t, checkChannelWithTimeout(createPullNotified, 100*time.Millisecond, isOurPR), "Duplicate notified pull created")
115+
assert.False(t, checkChannelWithTimeout(mergePullNotified, 100*time.Millisecond, isOurPR), "Duplicate notified pull merged")
126116
})
127117
}
128118

0 commit comments

Comments
 (0)