Commit 58fdcad9 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

multiconn: map + close on children close

parent fc5b0c29
......@@ -10,6 +10,9 @@ import (
u "github.com/jbenet/go-ipfs/util"
)
// MultiConnMap is for shorthand
type MultiConnMap map[u.Key]*MultiConn
// Duplex is a simple duplex channel
type Duplex struct {
In chan []byte
......@@ -160,10 +163,15 @@ func (c *MultiConn) fanInSingle(child Conn) {
// in case it still is in the map, remove it.
c.Lock()
delete(c.conns, child.ID())
connLen := len(c.conns)
c.Unlock()
c.Children().Done()
child.Children().Done()
if connLen == 0 {
c.Close() // close self if all underlying children are gone?
}
}()
for {
......
......@@ -293,3 +293,32 @@ func TestMulticonnSendUnderlying(t *testing.T) {
msgsFrom1.CheckDone(t)
msgsFrom2.CheckDone(t)
}
func TestMulticonnClose(t *testing.T) {
// t.Skip("fooo")
log.Info("TestMulticonnSendUnderlying")
ctx := context.Background()
c1, c2 := setupMultiConns(t, ctx)
for _, c := range c1.conns {
c.Close()
}
for _, c := range c2.conns {
c.Close()
}
timeout := time.After(100 * time.Millisecond)
select {
case <-c1.Closed():
case <-timeout:
t.Fatal("timeout")
}
select {
case <-c2.Closed():
case <-timeout:
t.Fatal("timeout")
}
}
......@@ -50,7 +50,7 @@ func TestSecureClose(t *testing.T) {
select {
case <-c1.Closed():
default:
t.Fatal("not done after cancel")
t.Fatal("not done after close")
}
c2.Close()
......@@ -58,7 +58,7 @@ func TestSecureClose(t *testing.T) {
select {
case <-c2.Closed():
default:
t.Fatal("not done after cancel")
t.Fatal("not done after close")
}
cancel() // close the listener :P
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment