From f1e24bf43b96625b9ad95bc37538ec56a44999b6 Mon Sep 17 00:00:00 2001 From: zenix Date: Fri, 10 Jun 2022 14:01:14 +0900 Subject: [PATCH 1/3] feature: add codecoverage and add race detection in go test, fix: fix race conditions --- .github/workflows/go.yml | 9 +++++++-- .gitignore | 3 +++ codecov.yml | 23 +++++++++++++++++++++++ pkg/depth/buffer.go | 5 ++--- pkg/util/reonce_test.go | 11 +++++++++-- 5 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 codecov.yml diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 2cc106ce0..81b92883e 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -63,10 +63,15 @@ jobs: run: go build -v ./cmd/bbgo - name: Test - run: go test ./pkg/... + run: go test -race -coverprofile coverage.txt -covermode atomic ./pkg/... - name: TestDnum - run: go test -tags dnum ./pkg/... + run: go test -race -coverprofile coverage_dnum.txt -covermode atomic -tags dnum ./pkg/... + + - name: Upload Coverage Report + uses: codecov/codecov-actions@v2 + with: + files: ./coverage.txt,./coverage_dnum.txt - name: Create dotenv file run: | diff --git a/.gitignore b/.gitignore index 1b3b385e6..543fe0cdc 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,6 @@ otp*png *.swp /pkg/backtest/assets.go + +coverage.txt +coverage_dum.txt diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 000000000..c4a76f25a --- /dev/null +++ b/codecov.yml @@ -0,0 +1,23 @@ +codecov: + require_ci_to_pass: true + +comment: + behavior: default + layout: "reach,diff,flags,files,footer" + require_changes: no + +parsers: + gcov: + branch_detection: + conditional: yes + loop: yes + method: no + macro: no + +coverage: + precision: 2 + round: down + range: "70...100" + +github_checks: + annotations: false diff --git a/pkg/depth/buffer.go b/pkg/depth/buffer.go index 26e74edf7..620743639 100644 --- a/pkg/depth/buffer.go +++ b/pkg/depth/buffer.go @@ -133,6 +133,8 @@ func (b *Buffer) AddUpdate(o types.SliceOrderBook, firstUpdateID int64, finalArg } func (b *Buffer) fetchAndPush() error { + b.mu.Lock() + defer b.mu.Unlock() book, finalUpdateID, err := b.fetcher() if err != nil { return err @@ -140,9 +142,6 @@ func (b *Buffer) fetchAndPush() error { log.Debugf("fetched depth snapshot, final update id %d", finalUpdateID) - b.mu.Lock() - defer b.mu.Unlock() - if len(b.buffer) > 0 { // the snapshot is too early if finalUpdateID < b.buffer[0].FirstUpdateID { diff --git a/pkg/util/reonce_test.go b/pkg/util/reonce_test.go index 7a18fc437..bd3528481 100644 --- a/pkg/util/reonce_test.go +++ b/pkg/util/reonce_test.go @@ -1,6 +1,7 @@ package util import ( + "sync" "testing" "time" @@ -10,14 +11,19 @@ import ( func TestReonce_DoAndReset(t *testing.T) { var cnt = 0 var reonce Reonce + var wgAll, wg sync.WaitGroup + wg.Add(1) + wgAll.Add(2) go reonce.Do(func() { t.Log("once #1") time.Sleep(10 * time.Millisecond) cnt++ + wg.Done() + wgAll.Done() }) // make sure it's locked - time.Sleep(10 * time.Millisecond) + wg.Wait() t.Logf("reset") reonce.Reset() @@ -25,8 +31,9 @@ func TestReonce_DoAndReset(t *testing.T) { t.Log("once #2") time.Sleep(10 * time.Millisecond) cnt++ + wgAll.Done() }) - time.Sleep(time.Second) + wgAll.Wait() assert.Equal(t, 2, cnt) } From 71aae0c4bb3149d5365f28c36e409b0dd2880237 Mon Sep 17 00:00:00 2001 From: zenix Date: Fri, 10 Jun 2022 14:05:56 +0900 Subject: [PATCH 2/3] fix: fix typo in codecov name --- .github/workflows/go.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 81b92883e..d46599eed 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -69,7 +69,7 @@ jobs: run: go test -race -coverprofile coverage_dnum.txt -covermode atomic -tags dnum ./pkg/... - name: Upload Coverage Report - uses: codecov/codecov-actions@v2 + uses: codecov/codecov-action@v3 with: files: ./coverage.txt,./coverage_dnum.txt From 1e67acd77ac5f4434f15cbb2677c05c087b8cb81 Mon Sep 17 00:00:00 2001 From: zenix Date: Fri, 10 Jun 2022 15:24:13 +0900 Subject: [PATCH 3/3] fix: set buffer period to allow buffer to get fully appended before the estimation in buffer_test --- pkg/depth/buffer_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/depth/buffer_test.go b/pkg/depth/buffer_test.go index 89f75d68b..9c33effdd 100644 --- a/pkg/depth/buffer_test.go +++ b/pkg/depth/buffer_test.go @@ -23,6 +23,7 @@ func TestDepthBuffer_ReadyState(t *testing.T) { }, }, 33, nil }) + buf.SetBufferingPeriod(time.Millisecond * 5) readyC := make(chan struct{}) buf.OnReady(func(snapshot types.SliceOrderBook, updates []Update) {