channels icon indicating copy to clipboard operation
channels copied to clipboard

The Len() of InfiniteChannel is incorrect

Open vegertar opened this issue 8 years ago • 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.

vegertar avatar Jul 30 '16 00:07 vegertar

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

vegertar avatar Jul 30 '16 00:07 vegertar

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.

eapache avatar Jul 30 '16 01:07 eapache