eapache/channels

The Len() of InfiniteChannel is incorrect

vegertar opened this issue · 2 comments

When I use InfiniteChannel, I found that Len() returned is a "case" buffered value, https://github.com/eapache/channels/blob/master/infinite_channel.go#L58

A little demo shown as bellow:

package main

import (
    "fmt"
    "github.com/eapache/channels"
)

func main() {
    ch := make(chan int)
    infiniteCh := channels.NewInfiniteChannel()
    channels.Unwrap(infiniteCh, ch)

    for i := 0; i < 1000; i++ {
        infiniteCh.In() <- i
    }
    infiniteCh.Close()
    fmt.Println(infiniteCh.Len())

    m := 0
    for _ = range ch {
        m += 1
    }
    fmt.Println(m)
}

The output is

999
1000

It seems like we cannot simply place an expression in blocking evaluation context.

BTW, I have noticed #18 also, but I think they aren't the same problem.

The problem is actually in the implementation of Unwrap which unconditional reads from the input channel before it may be able to write. I don't know if it is possible to fix off the top of my head.