swarm/network/stream: fix TestIntervals by waiting for subscription activation

This commit is contained in:
Janos Guljas 2018-02-28 12:56:36 +01:00
parent c63736067f
commit d17b9b04a0
2 changed files with 102 additions and 109 deletions

View file

@ -232,6 +232,11 @@ func (r *TestExternalRegistry) GetHashes(ctx context.Context, peerId discover.No
sub := notifier.CreateSubscription() sub := notifier.CreateSubscription()
go func() { go func() {
// if we begin sending event immediately some events
// will probably be dropped since the subscription ID might not be send to
// the client.
// ref: rpc/subscription_test.go#L65
time.Sleep(1 * time.Second)
for { for {
select { select {
case h := <-c.hashes: case h := <-c.hashes:

View file

@ -131,150 +131,138 @@ func testIntervals(t *testing.T, live bool, history *Range) {
} }
} }
liveHashesChan := make(chan []byte)
historyHashesChan := make(chan []byte)
var historySubscription *rpc.ClientSubscription
var liveSubscription *rpc.ClientSubscription
id := sim.IDs[1] id := sim.IDs[1]
err := sim.CallClient(id, func(client *rpc.Client) error { err := sim.CallClient(id, func(client *rpc.Client) error {
sid := sim.IDs[0]
err := streamTesting.WatchDisconnections(id, client, errc, quitC) err := streamTesting.WatchDisconnections(id, client, errc, quitC)
if err != nil { if err != nil {
return err return err
} }
ctx, cancel := context.WithTimeout(ctx, 100*time.Second) ctx, cancel := context.WithTimeout(ctx, 100*time.Second)
defer cancel() defer cancel()
sid := sim.IDs[0]
err = client.CallContext(ctx, nil, "stream_subscribeStream", sid, NewStream(externalStreamName, nil, live), history, Top) err = client.CallContext(ctx, nil, "stream_subscribeStream", sid, NewStream(externalStreamName, nil, live), history, Top)
if err != nil { if err != nil {
return err return err
} }
liveSubErrC := make(chan error) liveErrC := make(chan error)
historySubErrC := make(chan error)
go func() {
if live {
var err error
defer func() { liveSubErrC <- err }()
// live stream
liveSubscription, err = client.Subscribe(ctx, "stream", liveHashesChan, "getHashes", sid, NewStream(externalStreamName, nil, true))
if err != nil {
return
}
// we have got the channel, enable notifications
err = client.CallContext(ctx, nil, "stream_enableNotifications", sid, NewStream(externalStreamName, nil, true))
} else {
close(liveSubErrC)
}
}()
go func() {
if !live || history != nil {
var err error
defer func() { historySubErrC <- err }()
// history stream
historySubscription, err = client.Subscribe(ctx, "stream", historyHashesChan, "getHashes", sid, NewStream(externalStreamName, nil, false))
if err != nil {
return
}
// we have got the channel, enable notifications
err = client.CallContext(ctx, nil, "stream_enableNotifications", sid, NewStream(externalStreamName, nil, false))
} else {
close(historySubErrC)
}
}()
if err := <-liveSubErrC; err != nil {
return err
}
if err := <-historySubErrC; err != nil {
return err
}
return nil
})
if err != nil {
return err
}
historyErrC := make(chan error) historyErrC := make(chan error)
go func() { go func() {
defer close(historyErrC) if !live {
close(liveErrC)
if historySubscription == nil {
return return
} }
var err error
defer func() {
liveErrC <- err
}()
// live stream
liveHashesChan := make(chan []byte)
liveSubscription, err := client.Subscribe(ctx, "stream", liveHashesChan, "getHashes", sid, NewStream(externalStreamName, nil, true))
if err != nil {
return
}
defer liveSubscription.Unsubscribe()
i := externalStreamSessionAt
// we have subscribed, enable notifications
err = client.CallContext(ctx, nil, "stream_enableNotifications", sid, NewStream(externalStreamName, nil, true))
if err != nil {
return
}
for {
select {
case hash := <-liveHashesChan:
h := binary.BigEndian.Uint64(hash)
if h != i {
err = fmt.Errorf("expected live hash %d, got %d", i, h)
return
}
i++
if i > externalStreamMaxKeys {
return
}
case err = <-liveSubscription.Err():
return
case <-ctx.Done():
return
}
}
}()
go func() {
if live && history == nil {
close(historyErrC)
return
}
var err error
defer func() {
historyErrC <- err
}()
// history stream
historyHashesChan := make(chan []byte)
historySubscription, err := client.Subscribe(ctx, "stream", historyHashesChan, "getHashes", sid, NewStream(externalStreamName, nil, false))
if err != nil {
return
}
defer historySubscription.Unsubscribe() defer historySubscription.Unsubscribe()
i := history.From var i uint64
historyTo := externalStreamMaxKeys historyTo := externalStreamMaxKeys
if history != nil && history.To != 0 { if history != nil {
i = history.From
if history.To != 0 {
historyTo = history.To historyTo = history.To
} }
}
// we have subscribed, enable notifications
err = client.CallContext(ctx, nil, "stream_enableNotifications", sid, NewStream(externalStreamName, nil, false))
if err != nil {
return
}
for { for {
select { select {
case hash := <-historyHashesChan: case hash := <-historyHashesChan:
h := binary.BigEndian.Uint64(hash) h := binary.BigEndian.Uint64(hash)
if h != i { if h != i {
historyErrC <- fmt.Errorf("expected history hash %d, got %d", i, h) err = fmt.Errorf("expected history hash %d, got %d", i, h)
return return
} }
i++ i++
if i > historyTo { if i > historyTo {
return return
} }
case err := <-historySubscription.Err(): case err = <-historySubscription.Err():
historyErrC <- err return
case <-ctx.Done(): case <-ctx.Done():
return return
} }
} }
}() }()
liveErrC := make(chan error) if err := <-liveErrC; err != nil {
go func() {
defer close(liveErrC)
if liveSubscription == nil {
return
}
defer liveSubscription.Unsubscribe()
i := externalStreamSessionAt
for {
select {
case hash := <-liveHashesChan:
h := binary.BigEndian.Uint64(hash)
if h != i {
liveErrC <- fmt.Errorf("expected live hash %d, got %d", i, h)
return
}
i++
if i > externalStreamMaxKeys {
return
}
case err := <-liveSubscription.Err():
errc <- err
case <-ctx.Done():
return
}
}
}()
if err = <-historyErrC; err != nil {
return err return err
} }
return <-liveErrC if err := <-historyErrC; err != nil {
return err
}
return nil
})
return err
} }
check := func(ctx context.Context, id discover.NodeID) (bool, error) { check := func(ctx context.Context, id discover.NodeID) (bool, error) {
select { select {