@@ -2,6 +2,7 @@ package delivery
22
33import (
44 "context"
5+ "github.com/webhookx-io/webhookx/constants"
56 "github.com/webhookx-io/webhookx/test/helper/factory"
67 "strconv"
78 "testing"
@@ -253,6 +254,75 @@ var _ = Describe("delivery", Ordered, func() {
253254 })
254255 })
255256
257+ Context ("schedule task" , func () {
258+ var proxyClient * resty.Client
259+
260+ var app * app.Application
261+ var db * db.DB
262+
263+ BeforeAll (func () {
264+ endpoint := factory .Endpoint ()
265+ endpoint .Retry .Config .Attempts = []int64 {int64 (constants .TaskQueuePreScheduleTimeWindow .Seconds ()) + 3 }
266+ entitiesConfig := helper.EntitiesConfig {
267+ Endpoints : []* entities.Endpoint {& endpoint },
268+ Sources : []* entities.Source {factory .SourceP ()},
269+ }
270+ db = helper .InitDB (true , & entitiesConfig )
271+ proxyClient = helper .ProxyClient ()
272+
273+ app = utils .Must (helper .Start (map [string ]string {
274+ "WEBHOOKX_ADMIN_LISTEN" : "0.0.0.0:8080" ,
275+ "WEBHOOKX_PROXY_LISTEN" : "0.0.0.0:8081" ,
276+ "WEBHOOKX_WORKER_ENABLED" : "true" ,
277+ }))
278+ })
279+
280+ AfterAll (func () {
281+ app .Stop ()
282+ })
283+
284+ It ("scheudle task when conditions met" , func () {
285+ assert .Eventually (GinkgoT (), func () bool {
286+ resp , err := proxyClient .R ().
287+ SetBody (`{
288+ "event_type": "foo.bar",
289+ "data": {"key": "value"}
290+ }` ).
291+ Post ("/" )
292+ return err == nil && resp .StatusCode () == 200
293+ }, time .Second * 5 , time .Second )
294+
295+ time .Sleep (time .Second )
296+
297+ var attempt * entities.Attempt
298+ assert .Eventually (GinkgoT (), func () bool {
299+ list , err := db .Attempts .List (context .TODO (), & query.AttemptQuery {})
300+ if err != nil || len (list ) == 0 {
301+ return false
302+ }
303+ attempt = list [0 ]
304+ return attempt .Status == entities .AttemptStatusInit // should not be enqueued
305+ }, time .Second * 5 , time .Second )
306+
307+ result , err := db .DB .Exec ("UPDATE attempts set scheduled_at = $1, created_at = created_at - INTERVAL '30 SECOND' where id = $2" , time .Now (), attempt .ID )
308+ assert .NoError (GinkgoT (), err )
309+ row , err := result .RowsAffected ()
310+ assert .NoError (GinkgoT (), err )
311+ assert .Equal (GinkgoT (), int64 (1 ), row )
312+
313+ time .Sleep (time .Second )
314+
315+ app .Worker ().ProcessRequeue () // load db data that meets the conditions into task queue
316+
317+ assert .Eventually (GinkgoT (), func () bool {
318+ model , err := db .Attempts .Get (context .TODO (), attempt .ID )
319+ assert .NoError (GinkgoT (), err )
320+ return model .Status == entities .AttemptStatusSuccess
321+ }, time .Second * 5 , time .Second )
322+
323+ })
324+ })
325+
256326})
257327
258328func TestProxy (t * testing.T ) {
0 commit comments