From 485f7dde082c7d6aaad91fe612be11f714fa62fd Mon Sep 17 00:00:00 2001 From: Alexey Miroshkin Date: Fri, 26 Aug 2016 17:07:17 +0200 Subject: [PATCH] Fix data race in lexer and lexer test As described in #1898 'go test -race' detects a race in lexer code. This pacth fixes it and also add '-race' option to test target to prevent regression. --- Makefile | 2 +- promql/lex.go | 11 +++++++++-- promql/lex_test.go | 4 +--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 2484b7807..ce2af7d3e 100644 --- a/Makefile +++ b/Makefile @@ -38,7 +38,7 @@ check_license: test: @echo ">> running tests" - @$(GO) test -short $(pkgs) + @$(GO) test -race -short $(pkgs) format: @echo ">> formatting code" diff --git a/promql/lex.go b/promql/lex.go index 5bbe84364..297c83501 100644 --- a/promql/lex.go +++ b/promql/lex.go @@ -428,9 +428,16 @@ func (l *lexer) nextItem() item { // lex creates a new scanner for the input string. func lex(input string) *lexer { + return lexWithSeriesDesc(input, false) +} + +// lexWithSeriesDesc creates a new scanner for the input string +// and specify seriesDesc to prevent data race in tests +func lexWithSeriesDesc(input string, seriesDesc bool) *lexer { l := &lexer{ - input: input, - items: make(chan item), + input: input, + items: make(chan item), + seriesDesc: seriesDesc, } go l.run() return l diff --git a/promql/lex_test.go b/promql/lex_test.go index 824b4e00c..c497f896f 100644 --- a/promql/lex_test.go +++ b/promql/lex_test.go @@ -438,9 +438,7 @@ var tests = []struct { // for the parser to avoid duplicated effort. func TestLexer(t *testing.T) { for i, test := range tests { - l := lex(test.input) - l.seriesDesc = test.seriesDesc - + l := lexWithSeriesDesc(test.input, test.seriesDesc) out := []item{} for it := range l.items { out = append(out, it)