소스 검색

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