Skip to content

Commit

Permalink
feature: add codecoverage and add race detection in go test, fix: fix…
Browse files Browse the repository at this point in the history
… race conditions
  • Loading branch information
zenixls2 committed Jun 10, 2022
1 parent a4e3fd5 commit f1e24bf
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ otp*png

*.swp
/pkg/backtest/assets.go

coverage.txt
coverage_dum.txt
23 changes: 23 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 2 additions & 3 deletions pkg/depth/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,15 @@ 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
}

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 {
Expand Down
11 changes: 9 additions & 2 deletions pkg/util/reonce_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package util

import (
"sync"
"testing"
"time"

Expand All @@ -10,23 +11,29 @@ 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()

go reonce.Do(func() {
t.Log("once #2")
time.Sleep(10 * time.Millisecond)
cnt++
wgAll.Done()
})

time.Sleep(time.Second)
wgAll.Wait()
assert.Equal(t, 2, cnt)
}

0 comments on commit f1e24bf

Please sign in to comment.