@@ -25,6 +25,7 @@ use octobot::repo_version::RepoVersionRequest;
25
25
use octobot:: repos;
26
26
use octobot:: server:: github_handler:: GithubEventHandler ;
27
27
use octobot:: slack:: { self , SlackAttachmentBuilder } ;
28
+ use octobot:: util;
28
29
use octobot:: worker:: { WorkMessage , WorkSender } ;
29
30
30
31
use mocks:: mock_github:: MockGithub ;
@@ -56,7 +57,9 @@ impl GithubHandlerTest {
56
57
let rx = self . pr_merge_rx . take ( ) . unwrap ( ) ;
57
58
thread:: spawn ( move || {
58
59
for branch in branches {
59
- let msg = rx. recv_timeout ( timeout) . expect ( & format ! ( "expected to recv msg for branch: {}" , branch) ) ;
60
+ let msg = util:: recv_timeout ( & rx, timeout) . expect (
61
+ & format ! ( "expected to recv msg for branch: {}" , branch) ,
62
+ ) ;
60
63
match msg {
61
64
WorkMessage :: WorkItem ( req) => {
62
65
assert_eq ! ( branch, req. target_branch) ;
@@ -67,7 +70,61 @@ impl GithubHandlerTest {
67
70
} ;
68
71
}
69
72
70
- let last_message = rx. recv_timeout ( timeout) ;
73
+ let last_message = util:: recv_timeout ( & rx, timeout) ;
74
+ assert ! ( last_message. is_err( ) ) ;
75
+ } )
76
+ }
77
+
78
+ fn expect_will_force_push_notify ( & mut self , before_hash : & ' static str , after_hash : & ' static str ) -> JoinHandle < ( ) > {
79
+ let timeout = Duration :: from_millis ( 300 ) ;
80
+ let rx = self . force_push_rx . take ( ) . expect ( "force-push message" ) ;
81
+ thread:: spawn ( move || {
82
+ let msg = util:: recv_timeout ( & rx, timeout) . expect ( & format ! (
83
+ "expected to recv force-push for {} -> {}" ,
84
+ before_hash,
85
+ after_hash
86
+ ) ) ;
87
+ match msg {
88
+ WorkMessage :: WorkItem ( req) => {
89
+ assert_eq ! ( before_hash, req. before_hash) ;
90
+ assert_eq ! ( after_hash, req. after_hash) ;
91
+ }
92
+ _ => {
93
+ panic ! ( "Unexpected messages: {:?}" , msg) ;
94
+ }
95
+ } ;
96
+
97
+ let last_message = util:: recv_timeout ( & rx, timeout) ;
98
+ assert ! ( last_message. is_err( ) ) ;
99
+ } )
100
+ }
101
+
102
+ fn expect_will_not_force_push_notify ( & mut self ) -> JoinHandle < ( ) > {
103
+ let timeout = Duration :: from_millis ( 300 ) ;
104
+ let rx = self . force_push_rx . take ( ) . expect ( "force-push message" ) ;
105
+ thread:: spawn ( move || {
106
+ let last_message = util:: recv_timeout ( & rx, timeout) ;
107
+ assert ! ( last_message. is_err( ) ) ;
108
+ } )
109
+ }
110
+
111
+ fn expect_will_run_version_script ( & mut self , branch : & ' static str , commit_hash : & ' static str ) -> JoinHandle < ( ) > {
112
+ // Note: no expectations are set on mock_jira since we have stubbed out the background worker thread
113
+ let timeout = Duration :: from_millis ( 300 ) ;
114
+ let rx = self . repo_version_rx . take ( ) . unwrap ( ) ;
115
+ thread:: spawn ( move || {
116
+ let msg = util:: recv_timeout ( & rx, timeout) . expect ( & format ! ( "expected to recv version script msg" ) ) ;
117
+ match msg {
118
+ WorkMessage :: WorkItem ( req) => {
119
+ assert_eq ! ( branch, req. branch) ;
120
+ assert_eq ! ( commit_hash, req. commit_hash) ;
121
+ }
122
+ _ => {
123
+ panic ! ( "Unexpected messages: {:?}" , msg) ;
124
+ }
125
+ } ;
126
+
127
+ let last_message = util:: recv_timeout ( & rx, timeout) ;
71
128
assert ! ( last_message. is_err( ) ) ;
72
129
} )
73
130
}
@@ -1239,26 +1296,7 @@ fn test_push_force_notify() {
1239
1296
] ) ;
1240
1297
1241
1298
// Setup background thread to validate force-push msg
1242
- let expect_thread;
1243
- {
1244
- let timeout = Duration :: from_millis ( 300 ) ;
1245
- let rx = test. force_push_rx . take ( ) . expect ( "force-push message" ) ;
1246
- expect_thread = thread:: spawn ( move || {
1247
- let msg = rx. recv_timeout ( timeout) . expect ( & format ! ( "expected to recv msg" ) ) ;
1248
- match msg {
1249
- WorkMessage :: WorkItem ( req) => {
1250
- assert_eq ! ( "abcdef0000" , req. before_hash) ;
1251
- assert_eq ! ( "1111abcdef" , req. after_hash) ;
1252
- }
1253
- _ => {
1254
- panic ! ( "Unexpected messages: {:?}" , msg) ;
1255
- }
1256
- } ;
1257
-
1258
- let last_message = rx. recv_timeout ( timeout) ;
1259
- assert ! ( last_message. is_err( ) ) ;
1260
- } ) ;
1261
- }
1299
+ let expect_thread = test. expect_will_force_push_notify ( "abcdef0000" , "1111abcdef" ) ;
1262
1300
1263
1301
let resp = test. handler . handle_event ( ) . expect ( "handled event" ) ;
1264
1302
assert_eq ! ( ( StatusCode :: Ok , "push" . into( ) ) , resp) ;
@@ -1296,15 +1334,7 @@ fn test_push_force_notify_wip() {
1296
1334
// Note: not slack expectations here. It should not notify slack for WIP PRs.
1297
1335
1298
1336
// Setup background thread to validate force-push msg
1299
- let expect_thread;
1300
- {
1301
- let timeout = Duration :: from_millis ( 300 ) ;
1302
- let rx = test. force_push_rx . take ( ) . unwrap ( ) ;
1303
- expect_thread = thread:: spawn ( move || {
1304
- let last_message = rx. recv_timeout ( timeout) ;
1305
- assert ! ( last_message. is_err( ) ) ;
1306
- } ) ;
1307
- }
1337
+ let expect_thread = test. expect_will_not_force_push_notify ( ) ;
1308
1338
1309
1339
let resp = test. handler . handle_event ( ) . unwrap ( ) ;
1310
1340
assert_eq ! ( ( StatusCode :: Ok , "push" . into( ) ) , resp) ;
@@ -1358,15 +1388,7 @@ fn test_push_force_notify_ignored() {
1358
1388
] ) ;
1359
1389
1360
1390
// Setup background thread to validate force-push msg
1361
- let expect_thread;
1362
- {
1363
- let timeout = Duration :: from_millis ( 300 ) ;
1364
- let rx = test. force_push_rx . take ( ) . unwrap ( ) ;
1365
- expect_thread = thread:: spawn ( move || {
1366
- let last_message = rx. recv_timeout ( timeout) ;
1367
- assert ! ( last_message. is_err( ) ) ;
1368
- } ) ;
1369
- }
1391
+ let expect_thread = test. expect_will_not_force_push_notify ( ) ;
1370
1392
1371
1393
let resp = test. handler . handle_event ( ) . unwrap ( ) ;
1372
1394
assert_eq ! ( ( StatusCode :: Ok , "push" . into( ) ) , resp) ;
@@ -1416,11 +1438,11 @@ fn some_jira_commits() -> Vec<Commit> {
1416
1438
1417
1439
fn many_jira_commits ( ) -> Vec < Commit > {
1418
1440
let commit = Commit {
1419
- sha : "ffeedd00110011" . into ( ) ,
1420
- html_url : "http://commit/ffeedd00110011" . into ( ) ,
1421
- author : Some ( User :: new ( "bob-author" ) ) ,
1422
- commit : CommitDetails { message : "Fix [SER-1] Add the feature\n \n The body ([OTHER-123])" . into ( ) } ,
1423
- } ;
1441
+ sha : "ffeedd00110011" . into ( ) ,
1442
+ html_url : "http://commit/ffeedd00110011" . into ( ) ,
1443
+ author : Some ( User :: new ( "bob-author" ) ) ,
1444
+ commit : CommitDetails { message : "Fix [SER-1] Add the feature\n \n The body ([OTHER-123])" . into ( ) } ,
1445
+ } ;
1424
1446
1425
1447
return ( 0 ..21 ) . collect :: < Vec < u32 > > ( ) . into_iter ( ) . map ( |_| commit. clone ( ) ) . collect ( ) ;
1426
1448
}
@@ -1657,27 +1679,7 @@ fn test_jira_push_triggers_version_script() {
1657
1679
test. handler . data . commits = Some ( some_jira_push_commits ( ) ) ;
1658
1680
1659
1681
// Setup background thread to validate version msg
1660
- // Note: no expectations are set on mock_jira since we have stubbed out the background worker thread
1661
- let expect_thread;
1662
- {
1663
- let timeout = Duration :: from_millis ( 300 ) ;
1664
- let rx = test. repo_version_rx . take ( ) . unwrap ( ) ;
1665
- expect_thread = thread:: spawn ( move || {
1666
- let msg = rx. recv_timeout ( timeout) . expect ( & format ! ( "expected to recv msg" ) ) ;
1667
- match msg {
1668
- WorkMessage :: WorkItem ( req) => {
1669
- assert_eq ! ( "master" , req. branch) ;
1670
- assert_eq ! ( "1111abcdef" , req. commit_hash) ;
1671
- }
1672
- _ => {
1673
- panic ! ( "Unexpected messages: {:?}" , msg) ;
1674
- }
1675
- } ;
1676
-
1677
- let last_message = rx. recv_timeout ( timeout) ;
1678
- assert ! ( last_message. is_err( ) ) ;
1679
- } ) ;
1680
- }
1682
+ let expect_thread = test. expect_will_run_version_script ( "master" , "1111abcdef" ) ;
1681
1683
1682
1684
let resp = test. handler . handle_event ( ) . unwrap ( ) ;
1683
1685
assert_eq ! ( ( StatusCode :: Ok , "push" . into( ) ) , resp) ;
0 commit comments