Fixed a race condition inside the test.

This commit is contained in:
NI
2019-08-08 08:02:31 +08:00
parent ed75a9131f
commit fe185389fa

View File

@@ -58,6 +58,7 @@ func testDummyFetchChainGen(dd <-chan []byte) rw.FetchReaderFetcher {
} }
type dummyStreamCommand struct { type dummyStreamCommand struct {
lock sync.Mutex
l log.Logger l log.Logger
w StreamResponder w StreamResponder
downWait sync.WaitGroup downWait sync.WaitGroup
@@ -68,6 +69,7 @@ type dummyStreamCommand struct {
func newDummyStreamCommand( func newDummyStreamCommand(
l log.Logger, w StreamResponder, d network.Dial) FSMMachine { l log.Logger, w StreamResponder, d network.Dial) FSMMachine {
return &dummyStreamCommand{ return &dummyStreamCommand{
lock:sync.Mutex{},
l: l, l: l,
w: w, w: w,
downWait: sync.WaitGroup{}, downWait: sync.WaitGroup{},
@@ -82,6 +84,8 @@ func (d *dummyStreamCommand) Bootup(
) (FSMState, FSMError) { ) (FSMState, FSMError) {
d.downWait.Add(1) d.downWait.Add(1)
echoTrans:=d.echoTrans
go func() { go func() {
defer func() { defer func() {
d.w.Signal(HeaderClose) d.w.Signal(HeaderClose)
@@ -92,7 +96,7 @@ func (d *dummyStreamCommand) Bootup(
buf := make([]byte, 1024) buf := make([]byte, 1024)
for { for {
dt, dtOK := <-d.echoTrans dt, dtOK := <-echoTrans
if !dtOK { if !dtOK {
return return
@@ -137,6 +141,9 @@ func (d *dummyStreamCommand) run(
d.echoData = make([]byte, rLen) d.echoData = make([]byte, rLen)
copy(d.echoData, b) copy(d.echoData, b)
d.lock.Lock()
defer d.lock.Unlock()
if d.echoTrans != nil { if d.echoTrans != nil {
d.echoTrans <- d.echoData d.echoTrans <- d.echoData
} }
@@ -146,7 +153,11 @@ func (d *dummyStreamCommand) run(
func (d *dummyStreamCommand) Close() error { func (d *dummyStreamCommand) Close() error {
close(d.echoTrans) close(d.echoTrans)
d.lock.Lock()
d.echoTrans = nil d.echoTrans = nil
d.lock.Unlock()
d.downWait.Wait() d.downWait.Wait()
return nil return nil