瀏覽代碼

fix race condition in mock async producer

channels success and errors are not guaranteed to be closed before the channel
closed is closed. some tests, .e.g. TestProducerWithCheckerFunction relied on
this assumption, and produced race conditions (e.g. len(errors) is called after
producer.Close. with this change, close(mp.closed) is moved to the defer call,
after closing successes and errors.
Johannes Brüderl 7 年之前
父節點
當前提交
baa5962850
共有 1 個文件被更改,包括 1 次插入2 次删除
  1. 1 2
      mocks/async_producer.go

+ 1 - 2
mocks/async_producer.go

@@ -44,6 +44,7 @@ func NewAsyncProducer(t ErrorReporter, config *sarama.Config) *AsyncProducer {
 		defer func() {
 			close(mp.successes)
 			close(mp.errors)
+			close(mp.closed)
 		}()
 
 		for msg := range mp.input {
@@ -86,8 +87,6 @@ func NewAsyncProducer(t ErrorReporter, config *sarama.Config) *AsyncProducer {
 			mp.t.Errorf("Expected to exhaust all expectations, but %d are left.", len(mp.expectations))
 		}
 		mp.l.Unlock()
-
-		close(mp.closed)
 	}()
 
 	return mp