Skip to content

Commit

Permalink
Merge pull request #15 from atelieryuxki/main
Browse files Browse the repository at this point in the history
Add race and shuffle testing perspectives.
  • Loading branch information
yuxki committed Sep 30, 2023
2 parents 74fefb2 + 89454a5 commit e9e012a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
12 changes: 11 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,23 @@ jobs:
go-version: '1.20'
cache: false
- name: Go Test
run: go test -v -cover -coverprofile=cover.out ./...
run: go test -v -shuffle=on -cover -coverprofile=cover.out ./...

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

race:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.20'
cache: false
- name: Go Test
run: go test -race ./...

golangci:
runs-on: ubuntu-latest
Expand Down
33 changes: 25 additions & 8 deletions cache_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,29 @@ func TestCacheBatch_Run_DBNotChanged(t *testing.T) {
}
}

type StubChangebleCADBClient struct {
caName string
db []db.IntermidiateEntry
isFirstScan bool
}

func (s *StubChangebleCADBClient) Scan(ctx context.Context) ([]db.IntermidiateEntry, error) {
if s.isFirstScan {
s.isFirstScan = false
return s.db, nil
}
return []db.IntermidiateEntry{
{
Ca: "test-ca",
Serial: "8CA7B3FE5D7F007673C18CCC6A1F818085CDC5F5",
RevType: "R",
ExpDate: "230925234911Z",
RevDate: "230826234911Z",
CRLReason: "unspecified",
},
}, nil
}

func TestCacheBatch_Run_DBChanged(t *testing.T) {
t.Parallel()

Expand All @@ -227,7 +250,7 @@ func TestCacheBatch_Run_DBChanged(t *testing.T) {
CRLReason: "",
},
}
client := StubCADBClient{"test-ca", currentDB}
client := &StubChangebleCADBClient{"test-ca", currentDB, true}
responder := testCreateDelegatedResponder(t)
store := cache.NewResponseCacheStore()
batch, err := NewCacheBatch(
Expand All @@ -246,16 +269,10 @@ func TestCacheBatch_Run_DBChanged(t *testing.T) {

go batch.Run(context.TODO())
time.Sleep(time.Second * 1)
// Do first

// Update DB &Interval
currentDB[0].RevType = wantType
currentDB[0].RevDate = "230826234911Z"
currentDB[0].CRLReason = "unspecified"
// Interval
time.Sleep(time.Second * 2)

// Do Second

// Do Assertion
cache := testGetCache(t, targetSerial, store)

Expand Down
6 changes: 6 additions & 0 deletions pkg/cache/res_cache_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cache

import (
"math/big"
"sync"
"time"

"github.com/yuxki/dyocsp/pkg/date"
Expand All @@ -18,6 +19,7 @@ type ResponseCacheStore struct {
cacheMap map[string]ResponseCache
now date.Now
UpdatedAt time.Time
mu sync.Mutex
}

// NewResponseCacheStore creates and retruns new instance of ResponseCacheStore.
Expand Down Expand Up @@ -88,7 +90,9 @@ func (r *ResponseCacheStore) Update(caches []ResponseCache) []ResponseCache {
cacheMap[key] = caches[idx]
}

r.mu.Lock()
r.cacheMap = cacheMap
r.mu.Unlock()

return invalids
}
Expand All @@ -113,7 +117,9 @@ func (r *ResponseCacheStore) Get(serialNumber *big.Int) (*ResponseCache, bool) {

// Copy the cache map address for data protection
// from replacing address by the updating cache job
r.mu.Lock()
cm := r.cacheMap
r.mu.Unlock()
cache, ok = cm[key]
if !ok {
return nil, false
Expand Down

0 comments on commit e9e012a

Please sign in to comment.