FAQ
I just write a simple unit test for send data to channel, but it runs
error. Could anybody tell me why?

func TestChannel(t *testing.T) {
        ch := make(chan int)
        defer close(ch)
        ch <- 1

        log.Printf("ch.len:%d", len(ch))
        if len(ch) == 1 {
               t.Logf("test success")
        } else {
               t.Fatalf("test fail, val:%d, expect value: 1", len(ch))
        }
}


the error info is as follows:

goroutine 5 [chan send]:
communicate.TestChannel(0xc08205a1b0)
D:/workspace/naga/src/communicate/*future_test.go:11 +0xa4 ----> ch<-1*
testing.tRunner(0xc08205a1b0, 0x73d918)
C:/Go/src/testing/testing.go:473 +0x9f
created by testing.RunTests
C:/Go/src/testing/testing.go:582 +0x899
exit status 2


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Uli Kunitz at May 5, 2016 at 6:06 am
    Read the complete error message:

    fatal error: all goroutines are asleep - deadlock!


    You have created an unbuffered channel. That requires a goroutine active
    that receives from the channel. Since the length check implies, that you
    don't expect the integer to be read, you actually want to have a buffered
    channel. So change the first line of test to:

    ch := make(chan int, 1)

    Your test program will work then.

    Uli

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Steve tang at May 5, 2016 at 6:49 am
    Thanks, it is OK as your say:)
    On Thursday, May 5, 2016 at 2:06:48 PM UTC+8, Uli Kunitz wrote:

    Read the complete error message:

    fatal error: all goroutines are asleep - deadlock!


    You have created an unbuffered channel. That requires a goroutine active
    that receives from the channel. Since the length check implies, that you
    don't expect the integer to be read, you actually want to have a buffered
    channel. So change the first line of test to:

    ch := make(chan int, 1)

    Your test program will work then.

    Uli
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Yesudeep Mangalapilly at May 5, 2016 at 6:08 am
    IIRC, ch <- 1 would block until it can also be read from since the channel
    is synchronous
    and communication must happen in lockstep over such a channel. Where are
    you reading
    from the channel?
    On Wednesday, May 4, 2016 at 9:32:37 PM UTC-7, steve tang wrote:

    I just write a simple unit test for send data to channel, but it runs
    error. Could anybody tell me why?

    func TestChannel(t *testing.T) {
    ch := make(chan int)
    defer close(ch)
    ch <- 1

    log.Printf("ch.len:%d", len(ch))
    if len(ch) == 1 {
    t.Logf("test success")
    } else {
    t.Fatalf("test fail, val:%d, expect value: 1", len(ch))
    }
    }


    the error info is as follows:

    goroutine 5 [chan send]:
    communicate.TestChannel(0xc08205a1b0)
    D:/workspace/naga/src/communicate/*future_test.go:11 +0xa4 ----> ch<-1*
    testing.tRunner(0xc08205a1b0, 0x73d918)
    C:/Go/src/testing/testing.go:473 +0x9f
    created by testing.RunTests
    C:/Go/src/testing/testing.go:582 +0x899
    exit status 2

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 5, '16 at 4:32a
activeMay 5, '16 at 6:49a
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase