Skip to content

Commit 201f2cc

Browse files
committed
durableSleep test for send/recv
1 parent 40b7937 commit 201f2cc

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

dbos/workflows_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,6 +1790,66 @@ func TestSendRecv(t *testing.T) {
17901790
// Ensure total results match expected
17911791
assert.Equal(t, numReceivers, timeoutCount+errorCount, "expected total results to equal number of receivers")
17921792
})
1793+
1794+
t.Run("durableSleep", func(t *testing.T) {
1795+
// Clear events before starting
1796+
receiveIdempotencyStartEvent.Clear()
1797+
receiveIdempotencyStopEvent.Clear()
1798+
1799+
// First execution: Start workflow that will timeout after 3 seconds and then block
1800+
workflowID := uuid.NewString()
1801+
startTime := time.Now()
1802+
1803+
handle1, err := RunWorkflow(dbosCtx, receiveIdempotencyWorkflow, "durable-sleep-topic", WithWorkflowID(workflowID))
1804+
require.NoError(t, err, "failed to start first receive workflow")
1805+
1806+
// Wait for the workflow to signal it has completed the Recv call (which includes the sleep)
1807+
receiveIdempotencyStartEvent.Wait()
1808+
receiveIdempotencyStartEvent.Clear()
1809+
1810+
// Verify it took at least close to 3 seconds to reach this point (the Recv timeout)
1811+
elapsed := time.Since(startTime)
1812+
require.GreaterOrEqual(t, elapsed, 2900*time.Millisecond, "expected workflow to sleep for close to 3 seconds, but elapsed time was %v", elapsed)
1813+
1814+
// Now the workflow is blocked on receiveIdempotencyStopEvent.Wait()
1815+
// Let's recover it to test that the sleep is not repeated
1816+
recoveredHandles, err := recoverPendingWorkflows(dbosCtx.(*dbosContext), []string{"local"})
1817+
require.NoError(t, err, "failed to recover pending workflows")
1818+
require.Len(t, recoveredHandles, 1, "expected 1 recovered handle, got %d", len(recoveredHandles))
1819+
1820+
// The recovered workflow should proceed quickly since it already completed the sleep
1821+
receiveIdempotencyStartEvent.Wait()
1822+
1823+
// Verify that the recovery was fast (no additional 3-second sleep)
1824+
secondElapsed := time.Since(startTime)
1825+
additionalTime := secondElapsed - elapsed
1826+
require.Less(t, additionalTime, 500*time.Millisecond, "expected recovery to be fast (additional time less than 500ms), but additional time was %v", additionalTime)
1827+
1828+
// Complete the workflow
1829+
receiveIdempotencyStopEvent.Set()
1830+
1831+
// Get results from both handles - they should be the same (empty string due to timeout)
1832+
result1, err := handle1.GetResult()
1833+
require.NoError(t, err, "failed to get result from first workflow")
1834+
require.Equal(t, "", result1, "expected empty result from first workflow due to timeout")
1835+
1836+
result2, err := recoveredHandles[0].GetResult()
1837+
require.NoError(t, err, "failed to get result from recovered workflow")
1838+
require.Equal(t, result1, result2, "expected both workflow results to be the same")
1839+
1840+
// Verify that there are exactly 2 steps: recv and sleep
1841+
steps, err := GetWorkflowSteps(dbosCtx, workflowID)
1842+
require.NoError(t, err, "failed to get workflow steps")
1843+
require.Len(t, steps, 2, "expected 2 steps (recv + sleep), got %d", len(steps))
1844+
1845+
// First step should be recv
1846+
require.Equal(t, 0, steps[0].StepID, "expected first step ID to be 0")
1847+
require.Equal(t, "DBOS.recv", steps[0].StepName, "expected first step to be recv")
1848+
1849+
// Second step should be sleep
1850+
require.Equal(t, 1, steps[1].StepID, "expected second step ID to be 1")
1851+
require.Equal(t, "DBOS.sleep", steps[1].StepName, "expected second step to be sleep")
1852+
})
17931853
}
17941854

17951855
var (
@@ -2340,6 +2400,7 @@ func TestSetGetEvent(t *testing.T) {
23402400

23412401
// Wait for the workflow to signal it has completed the GetEvent call (which includes the sleep)
23422402
getEventStartIdempotencyEvent.Wait()
2403+
getEventStartIdempotencyEvent.Clear()
23432404

23442405
// Verify it took at least close to 3 seconds to reach this point (the GetEvent timeout)
23452406
elapsed := time.Since(startTime)

0 commit comments

Comments
 (0)