# Writing unit test in golang for workflow that waits for signal

**URL:** <https://community.temporal.io/t/writing-unit-test-in-golang-for-workflow-that-waits-for-signal/2330>\
**Category:** Community Support\
**Tags:** go-sdk, testing\
**Created:** [June 8, 2021, 4:54pm UTC](https://community.temporal.io/t/writing-unit-test-in-golang-for-workflow-that-waits-for-signal/2330 "2021-06-08T16:54:23Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![stuartccng](https://avatars.discourse-cdn.com/v4/letter/s/e79b87/32.png) [@stuartccng](https://community.temporal.io/u/stuartccng)\
**Post date:** [June 8, 2021, 4:54pm UTC](https://community.temporal.io/t/writing-unit-test-in-golang-for-workflow-that-waits-for-signal/2330/1 "2021-06-08T16:54:23Z")

</div>

Hello. I am trying to write unit test for a workflow which waits on incoming signals. (basically a workflow select).

I am trying to follow the example here:

> **[Testing Workflows | Temporal documentation](https://docs.temporal.io/docs/go/testing/)**
>
> The Temporal Go SDK provides a test framework to facilitate testing Workflow implementations.

I get the deadline exceeded error when running or debugging (as soon as I hit select). I even try setTestTimeout to some huge value and it blows up right away.

I see there is an equivalent issue in java here - [Issues with Unit Testing a Workflow with signal method](https://community.temporal.io/t/issues-with-unit-testing-a-workflow-with-signal-method/1735)

Is there an equivalent work around for go? Thanks

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [June 8, 2021, 5:46pm UTC](https://community.temporal.io/t/writing-unit-test-in-golang-for-workflow-that-waits-for-signal/2330/2 "2021-06-08T17:46:02Z")

</div>

> [@Testing Workflow that waits for Signal](https://community.temporal.io/t/testing-workflow-that-waits-for-signal/1037/6):
>
> [RegisterDelayedCallback](https://github.com/temporalio/sdk-go/blob/b8d2ed454fbb56484d309a91e394beb8a3677154/internal/workflow_testsuite.go#L723) registers a callback to fire after a delay. The call itself is not blocking. Try changing it to: s.env.RegisterDelayedCallback(func() { s.env.SignalWorkflow("signal-name", "signal-value") }, time.Minute) Also, you probably want to adjust the timer from one-second delay if you want to fire it after the signal.

---

<div class="post-metadata">

**Author:** ![stuartccng](https://avatars.discourse-cdn.com/v4/letter/s/e79b87/32.png) [@stuartccng](https://community.temporal.io/u/stuartccng)\
**Post date:** [June 8, 2021, 6:43pm UTC](https://community.temporal.io/t/writing-unit-test-in-golang-for-workflow-that-waits-for-signal/2330/3 "2021-06-08T18:43:02Z")

</div>

Sorry, which timeout are your referring to?

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [June 8, 2021, 6:44pm UTC](https://community.temporal.io/t/writing-unit-test-in-golang-for-workflow-that-waits-for-signal/2330/4 "2021-06-08T18:44:42Z")

</div>

Are you using `registeDelayedCallback` to send signals?

---

<div class="post-metadata">

**Author:** ![stuartccng](https://avatars.discourse-cdn.com/v4/letter/s/e79b87/32.png) [@stuartccng](https://community.temporal.io/u/stuartccng)\
**Post date:** [June 8, 2021, 6:46pm UTC](https://community.temporal.io/t/writing-unit-test-in-golang-for-workflow-that-waits-for-signal/2330/5 "2021-06-08T18:46:22Z")

</div>

I have changed it.

I got this error

workflow execution error (type: workflowName, workflowID: default-test-workflow-id, runID: default-test-run-id): deadline exceeded (type: ScheduleToClose)

Do I need to set some startWorkflowOptions as well?

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [June 8, 2021, 6:48pm UTC](https://community.temporal.io/t/writing-unit-test-in-golang-for-workflow-that-waits-for-signal/2330/6 "2021-06-08T18:48:18Z")

</div>

`ScheduleToClose` is an activity timeout. So it looks like some activity is timing out in your unit test.

---

<div class="post-metadata">

**Author:** ![stuartccng](https://avatars.discourse-cdn.com/v4/letter/s/e79b87/32.png) [@stuartccng](https://community.temporal.io/u/stuartccng)\
**Post date:** [June 8, 2021, 6:53pm UTC](https://community.temporal.io/t/writing-unit-test-in-golang-for-workflow-that-waits-for-signal/2330/7 "2021-06-08T18:53:28Z")

</div>

ah i see… I probably need to mock return for all activities first thanks

---

<div class="post-metadata">

**Author:** ![stuartccng](https://avatars.discourse-cdn.com/v4/letter/s/e79b87/32.png) [@stuartccng](https://community.temporal.io/u/stuartccng)\
**Post date:** [June 8, 2021, 7:03pm UTC](https://community.temporal.io/t/writing-unit-test-in-golang-for-workflow-that-waits-for-signal/2330/8 "2021-06-08T19:03:25Z")

</div>

I will still getting that error, even if I mocked all activities like this

s.env.OnActivity(MigrationCompleteStageActivity, mock.Anything, mock.Anything, mock.Anything).Return(nil)

Question, does the initial parameter ctx for each activity need to be included in the list of mock argument?

Looking at this further, seems my signals never got received by my workflow under test, it just dies panic as soon as I call selector.Select(ctx) inside the workflow

in debug console, I got this

suite.go:63: test panicked: Potential deadlock detected: workflow goroutine “root” didn’t yield for over a second. Maybe as select is blocking?

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [June 8, 2021, 7:56pm UTC](https://community.temporal.io/t/writing-unit-test-in-golang-for-workflow-that-waits-for-signal/2330/9 "2021-06-08T19:56:51Z")

</div>

Are you using any synchronization in the workflow code? Can it block on some non Temporal API? Does it call any external code directly?

Would you ensure that the workflow code obeys all the [restrictions](https://docs.temporal.io/docs/go/workflows#how-to-write-workflow-code)?

---

<div class="post-metadata">

**Author:** ![stuartccng](https://avatars.discourse-cdn.com/v4/letter/s/e79b87/32.png) [@stuartccng](https://community.temporal.io/u/stuartccng)\
**Post date:** [June 8, 2021, 8:13pm UTC](https://community.temporal.io/t/writing-unit-test-in-golang-for-workflow-that-waits-for-signal/2330/10 "2021-06-08T20:13:19Z")

</div>

all the synchronization is done via workflow versions of the go calls. I do launch go routines via workflow.go and also has workflow.waitGroup…

The only other thing would be logging

oh, I do have a for statement wrapping select so it can keep looping

for {  
s.Select(ctx)  
// do various things  
}

Basically keep waiting for new signals until certain conditions are met then exit workflow

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [June 8, 2021, 8:55pm UTC](https://community.temporal.io/t/writing-unit-test-in-golang-for-workflow-that-waits-for-signal/2330/11 "2021-06-08T20:55:01Z")

</div>

The “Potential deadlock detected: workflow goroutine “root” didn’t yield for over a second” error indicates that the workflow is probably spinning on something. It means that the workflow thread didn’t call some Temporal API for at least a second.

---

<div class="post-metadata">

**Author:** ![stuartccng](https://avatars.discourse-cdn.com/v4/letter/s/e79b87/32.png) [@stuartccng](https://community.temporal.io/u/stuartccng)\
**Post date:** [June 8, 2021, 9:02pm UTC](https://community.temporal.io/t/writing-unit-test-in-golang-for-workflow-that-waits-for-signal/2330/12 "2021-06-08T21:02:05Z")

</div>

I figured out why, I need to RegisterDelayedCallback first before running the workflow. Thanks for your help!

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [June 9, 2021, 12:58am UTC](https://community.temporal.io/t/writing-unit-test-in-golang-for-workflow-that-waits-for-signal/2330/13 "2021-06-09T00:58:20Z")

</div>

Filed an [issue](https://github.com/temporalio/sdk-go/issues/458) to fail on such incorrect usage.
