Created
December 11, 2014 19:31
-
-
Save iamatypeofwalrus/84b6c7d946a6a4143a1d to your computer and use it in GitHub Desktop.
Check if a Go Channel is open or closed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// An intersting pattern for testing, but you can use the check anywhere | |
import "testing" | |
func TestCheckingChannel(t *testing.T) { | |
stop := make(chan bool) | |
// Testing some fucntion that SHOULD close the channel | |
func (stop chan bool) { | |
close(chan) | |
}(stop) | |
// Make sure that the function does close the channel | |
_, ok := (<-stop) | |
// If we can recieve on the channel then it is NOT closed | |
if ok { | |
t.Error("Channel is not closed") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks , you save my day.