Skip to content

Commit

Permalink
Merge pull request #41 Removed dependencies from libary code
Browse files Browse the repository at this point in the history
  • Loading branch information
rekby authored Aug 7, 2024
2 parents e2fe036 + 63b4111 commit 3b688c4
Show file tree
Hide file tree
Showing 18 changed files with 354 additions and 296 deletions.
17 changes: 16 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@ jobs:
- name: Build
run: go build -v ./...

- name: Build examples
if: ${{ matrix.goVersion == env.GO_VERSION }}
run: cd examples; go build -v ./...

- name: Test
run: go test ./...

- name: Test with coverage profiler
- name: Test examples
if: ${{ matrix.goVersion == env.GO_VERSION }}
run: cd examples; go test ./...

- name: Test library with coverage profiler
if: ${{ matrix.goVersion == env.GO_VERSION }}
run: go test -test.count=10 -race -covermode atomic -coverprofile=covprofile.out ./...

Expand All @@ -42,6 +50,13 @@ jobs:
with:
version: latest

- name: golangci-lint examples
if: ${{ matrix.goVersion == env.GO_VERSION }}
uses: golangci/golangci-lint-action@v2
with:
version: latest
working-directory: examples

- name: Coveralls install goveralls
if: ${{ matrix.goVersion == env.GO_VERSION }}
run: go install github.com/mattn/goveralls@latest
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Go Fixtures
===========

Inspired by pytest fixtures.
Inspired by pytest fixtures. No dependencies.

[Examples](https://github.com/rekby/fixenv/tree/master/examples)

Expand Down
85 changes: 35 additions & 50 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ import (
"sync/atomic"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

const waitTime = time.Second / 10

func TestCache_DeleteKeys(t *testing.T) {
t.Run("simple", func(t *testing.T) {
at := assert.New(t)

c := newCache()
k1 := cacheKey("k1")
k2 := cacheKey("k2")
Expand All @@ -33,25 +29,23 @@ func TestCache_DeleteKeys(t *testing.T) {

c.DeleteKeys(k1, k2)
_, ok := c.get(k1)
at.False(ok)
requireFalse(t, ok)
_, ok = c.get(k2)
at.False(ok)
requireFalse(t, ok)
res, ok := c.get(k3)
at.True(ok)
at.Equal(val1, res.res.Value)
requireTrue(t, ok)
requireEquals(t, val1, res.res.Value.(string))

val2 := "test2"
c.setOnce(k1, func() (res *Result, err error) {
return NewResult(val2), nil
})
res, ok = c.get(k1)
at.True(ok)
at.Equal(val2, res.res.Value)
requireTrue(t, ok)
requireEquals(t, val2, res.res.Value.(string))
})

t.Run("mutex", func(t *testing.T) {
at := assert.New(t)

c := newCache()
c.setOnce("asd", func() (res *Result, err error) {
return NewResult(nil), nil
Expand All @@ -66,45 +60,42 @@ func TestCache_DeleteKeys(t *testing.T) {
}()

time.Sleep(waitTime)
at.Len(c.store, 1)
at.Len(c.setLocks, 1)
requireEquals(t, len(c.store), 1)
requireEquals(t, len(c.setLocks), 1)

c.m.RUnlock()
wg.Wait()
at.Len(c.store, 0)
at.Len(c.setLocks, 0)
requireEquals(t, len(c.store), 0)
requireEquals(t, len(c.setLocks), 0)
})
}

func TestCache_Get(t *testing.T) {
t.Run("simple", func(t *testing.T) {
at := assert.New(t)
c := newCache()

_, ok := c.get("qwe")
at.False(ok)
requireFalse(t, ok)

c.store["asd"] = cacheVal{res: NewResult("val")}

res, ok := c.get("asd")
at.True(ok)
at.Equal(cacheVal{res: NewResult("val")}, res)
requireTrue(t, ok)
requireEquals(t, cacheVal{res: NewResult("val")}, res)
})

t.Run("read_mutex", func(t *testing.T) {
at := assert.New(t)
c := newCache()
c.setOnce("asd", func() (res *Result, err error) {
return NewResult(nil), nil
})
c.m.RLock()
_, ok := c.get("asd")
c.m.RUnlock()
at.True(ok)
requireTrue(t, ok)
})

t.Run("write_mutex", func(t *testing.T) {
at := assert.New(t)
c := newCache()
c.setOnce("asd", func() (res *Result, err error) {
return NewResult(nil), nil
Expand All @@ -119,42 +110,39 @@ func TestCache_Get(t *testing.T) {
}()

time.Sleep(waitTime)
at.False(ok)
requireFalse(t, ok)

c.m.Unlock()
wg.Wait()

at.True(ok)
requireTrue(t, ok)
})

}

func TestCache_SetOnce(t *testing.T) {
t.Run("save_new_key", func(t *testing.T) {
at := assert.New(t)

c := newCache()
cnt := 0
key1 := cacheKey("1")
c.setOnce(key1, func() (res *Result, err error) {
cnt++
return NewResult(1), nil
})
at.Equal(1, cnt)
at.Equal(1, c.store[key1].res.Value)
at.NoError(c.store[key1].err)
requireEquals(t, 1, cnt)
requireEquals(t, 1, c.store[key1].res.Value)
noError(t, c.store[key1].err)

c.setOnce(key1, func() (res *Result, err error) {
cnt++
return NewResult(2), nil
})
at.Equal(1, cnt)
at.Equal(1, c.store[key1].res.Value)
at.NoError(c.store[key1].err)
requireEquals(t, 1, cnt)
requireEquals(t, 1, c.store[key1].res.Value)
noError(t, c.store[key1].err)
})

t.Run("second_set_val", func(t *testing.T) {
at := assert.New(t)
c := newCache()
key1 := cacheKey("1")
key2 := cacheKey("2")
Expand All @@ -167,16 +155,15 @@ func TestCache_SetOnce(t *testing.T) {
cnt++
return NewResult(2), nil
})
at.Equal(2, cnt)
at.Equal(1, c.store[key1].res.Value)
at.NoError(c.store[key1].err)
at.Equal(2, c.store[key2].res.Value)
at.NoError(c.store[key2].err)
requireEquals(t, 2, cnt)
requireEquals(t, 1, c.store[key1].res.Value)
noError(t, c.store[key1].err)
requireEquals(t, 2, c.store[key2].res.Value)
noError(t, c.store[key2].err)
})

// exit without return value
t.Run("exit_without_return", func(t *testing.T) {
at := assert.New(t)
c := newCache()
key := cacheKey("3")
var wg sync.WaitGroup
Expand All @@ -190,12 +177,11 @@ func TestCache_SetOnce(t *testing.T) {
}()
wg.Wait()

at.Nil(c.store[key].res)
at.Error(c.store[key].err)
requireNil(t, c.store[key].res)
isError(t, c.store[key].err)
})

t.Run("second_func_same_key_wait", func(t *testing.T) {
at := assert.New(t)
c := newCache()
key := cacheKey("1")

Expand Down Expand Up @@ -239,8 +225,8 @@ func TestCache_SetOnce(t *testing.T) {
// second not work until first finished
doneSecondVal := atomic.LoadInt64(&doneSecond)
_, ok := c.store[key]
at.False(ok)
at.Equal(int64(0), doneSecondVal)
requireFalse(t, ok)
requireEquals(t, int64(0), doneSecondVal)

close(firstMuNeedFinish)

Expand All @@ -251,12 +237,11 @@ func TestCache_SetOnce(t *testing.T) {
<-secondFinished

doneSecondVal = atomic.LoadInt64(&doneSecond)
at.Equal(1, c.store[key].res.Value)
at.Equal(int64(1), doneSecondVal)
requireEquals(t, 1, c.store[key].res.Value)
requireEquals(t, int64(1), doneSecondVal)
})

t.Run("second_func_other_key_work", func(t *testing.T) {
at := assert.New(t)
c := newCache()
key1 := cacheKey("1")
key2 := cacheKey("2")
Expand Down Expand Up @@ -295,8 +280,8 @@ func TestCache_SetOnce(t *testing.T) {
// wait first func finished
<-firstMuFinished

at.Equal(1, c.store[key1].res.Value)
at.Equal(2, c.store[key2].res.Value)
requireEquals(t, 1, c.store[key1].res.Value)
requireEquals(t, 2, c.store[key2].res.Value)
})
}

Expand Down
Loading

0 comments on commit 3b688c4

Please sign in to comment.