Skip to content

Commit

Permalink
Corrected tests, and updated actions
Browse files Browse the repository at this point in the history
  • Loading branch information
benjivesterby committed Mar 18, 2022
1 parent 95a45ab commit 18e41c0
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 96 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
name: Execute Benchmarks
strategy:
matrix:
go-version: [1.18.0-beta1]
go-version: [1.18.x]
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v2
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
name: "Build"
strategy:
matrix:
go-version: [1.18.0-beta1]
go-version: [1.18.x]
platform: [ubuntu-latest, macos-latest, windows-latest]
fail-fast: true
runs-on: ${{ matrix.platform }}
Expand All @@ -27,7 +27,7 @@ jobs:
needs: [build]
strategy:
matrix:
go-version: [1.18.0-beta1]
go-version: [1.18.x]
platform: [ubuntu-latest, macos-latest, windows-latest]
fail-fast: true
runs-on: ${{ matrix.platform }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [1.18.0-beta1]
go-version: [1.18.x]
steps:
- name: Checkout code
uses: actions/checkout@v2.4.0
Expand Down
22 changes: 2 additions & 20 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
name: "Build & Unit Tests"
strategy:
matrix:
go-version: [1.18.0-beta1]
go-version: [1.18.x]
platform: [ubuntu-latest, macos-latest, windows-latest]
fail-fast: true
runs-on: ${{ matrix.platform }}
Expand All @@ -26,26 +26,8 @@ jobs:
run: go build ./...
- name: Test
run: go test -race -failfast ./...
codeql:
name: "CodeQL Check"
needs: [build]
strategy:
matrix:
go-version: [1.18.0-beta1]
platform: [ubuntu-latest, macos-latest, windows-latest]
fail-fast: true
runs-on: ${{ matrix.platform }}
steps:
- name: Checkout code
uses: actions/checkout@v2.4.0
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: go
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
release:
needs: [build, codeql]
needs: [build]
name: "Tagged Release"
runs-on: "ubuntu-latest"
steps:
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repos:
rev: v1.43.0
hooks:
- id: golangci-lint
- repo: git://github.com/Bahjat/pre-commit-golang
- repo: https://github.com/Bahjat/pre-commit-golang
rev: c3086eea8af86847dbdff2e46b85a5fe3c9d9656 # pragma: allowlist secret
hooks:
- id: go-unit-tests
Expand Down
77 changes: 31 additions & 46 deletions cache_exported_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ func Test_NewCache(t *testing.T) {

for name, test := range testdata {
t.Run(name, func(t *testing.T) {
c := NewCache(test.Context, test.timeout, test.extend)
c := NewCache[int, any](test.Context, test.timeout, test.extend)

che, ok := c.(*cache)
che, ok := c.(*cache[int, any])
if !ok {
t.Fatal("Invalid internal cache type")
}
Expand Down Expand Up @@ -84,9 +84,9 @@ func Test_Delete(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

c := NewCache(ctx, time.Hour, false)
c := NewCache[string, string](ctx, time.Hour, false)

che, ok := c.(*cache)
che, ok := c.(*cache[string, string])
if !ok {
t.Fatal("Invalid internal cache type")
}
Expand Down Expand Up @@ -125,11 +125,11 @@ func Test_Delete(t *testing.T) {
func Test_Get(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := NewCache(ctx, time.Hour, false)
c := NewCache[string, any](ctx, time.Hour, false)

testdata := map[string]struct {
key interface{}
value interface{}
key string
value any
}{
"nil": {
"test",
Expand Down Expand Up @@ -177,9 +177,9 @@ func Test_Get_nilmap(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

c := NewCache(ctx, time.Hour, false)
c := NewCache[string, any](ctx, time.Hour, false)

che, ok := c.(*cache)
che, ok := c.(*cache[string, any])
if !ok {
t.Fatal("Invalid internal cache type")
}
Expand All @@ -200,7 +200,7 @@ func Test_Get_novalue(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

c := NewCache(ctx, time.Hour, false)
c := NewCache[string, any](ctx, time.Hour, false)

v, ok := c.Get(ctx, "test")
if ok || v != nil {
Expand All @@ -211,9 +211,9 @@ func Test_Get_novalue(t *testing.T) {
func Test_Get_closedctx(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())

c := NewCache(ctx, time.Hour, false)
c := NewCache[string, any](ctx, time.Hour, false)

che, ok := c.(*cache)
che, ok := c.(*cache[string, any])
if !ok {
t.Fatal("Invalid internal cache type")
}
Expand All @@ -226,7 +226,7 @@ func Test_Get_closedctx(t *testing.T) {

// This will create a fake struct which has a channel
// that will always block on read
che.values["test"] = &rw{}
che.values["test"] = &rw[any]{}

v, ok := c.Get(ctx, "test")
if ok || v != nil {
Expand All @@ -237,7 +237,7 @@ func Test_Get_closedctx(t *testing.T) {
func Test_Set(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := NewCache(ctx, time.Hour, false)
c := NewCache[string, string](ctx, time.Hour, false)

for i := 0; i < 1000; i++ {
value := randomdata.SillyName()
Expand All @@ -248,13 +248,8 @@ func Test_Set(t *testing.T) {
t.Fatal("expected key to exist")
}

out, ok := v.(string)
if !ok {
t.Fatalf("expected stored string, got %T", v)
}

if out != value {
t.Fatalf("expected value %s; got %s", value, out)
if v != value {
t.Fatalf("expected value %s; got %s", value, v)
}
}
}
Expand All @@ -263,9 +258,9 @@ func Test_Set_nilmap(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

c := NewCache(ctx, time.Hour, false)
c := NewCache[string, any](ctx, time.Hour, false)

che, ok := c.(*cache)
che, ok := c.(*cache[string, any])
if !ok {
t.Fatal("Invalid internal cache type")
}
Expand All @@ -286,7 +281,7 @@ func Test_Set_existing_update(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

c := NewCache(ctx, time.Hour, false)
c := NewCache[string, string](ctx, time.Hour, false)

c.Set(ctx, key, value)

Expand All @@ -297,13 +292,8 @@ func Test_Set_existing_update(t *testing.T) {
t.Fatal("expected to find value")
}

out, ok := v.(string)
if !ok {
t.Fatalf("expected string; got %T", v)
}

if out != testvalue {
t.Fatalf("expected [%s]; got [%s]", testvalue, out)
if v != testvalue {
t.Fatalf("expected [%s]; got [%s]", testvalue, v)
}

testvalue = randomdata.SillyName()
Expand All @@ -320,9 +310,9 @@ func Test_Set_closedctx(t *testing.T) {

ctx2, cancel2 := context.WithCancel(context.Background())

c := NewCache(ctx, time.Hour, false)
c := NewCache[string, any](ctx, time.Hour, false)

che, ok := c.(*cache)
che, ok := c.(*cache[string, any])
if !ok {
t.Fatal("Invalid internal cache type")
}
Expand All @@ -333,7 +323,7 @@ func Test_Set_closedctx(t *testing.T) {

// This will create a fake struct which has a channel
// that will always block on read
che.values["test"] = &rw{}
che.values["test"] = &rw[any]{}

cancel2()
err := c.Set(ctx2, "test", "value")
Expand All @@ -345,7 +335,7 @@ func Test_Set_closedctx(t *testing.T) {
func Test_SetTTL(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := NewCache(ctx, time.Hour, false)
c := NewCache[string, string](ctx, time.Hour, false)

for i := 0; i < 1000; i++ {
value := randomdata.SillyName()
Expand All @@ -359,21 +349,16 @@ func Test_SetTTL(t *testing.T) {
t.Fatal("expected key to exist")
}

out, ok := v.(string)
if !ok {
t.Fatalf("expected stored string, got %T", v)
}

if out != value {
t.Fatalf("expected value %s; got %s", value, out)
if v != value {
t.Fatalf("expected value %s; got %s", value, v)
}
}
}

func Test_Set_expiration(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := NewCache(ctx, time.Hour, false)
c := NewCache[string, string](ctx, time.Hour, false)

testdata := map[string]struct {
expected time.Duration
Expand Down Expand Up @@ -425,7 +410,7 @@ func Test_Set_expiration(t *testing.T) {
func Test_Set_extension(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := NewCache(ctx, time.Hour, true)
c := NewCache[string, string](ctx, time.Hour, true)

testdata := map[string]struct {
expected time.Duration
Expand Down Expand Up @@ -476,7 +461,7 @@ func Test_Set_extension(t *testing.T) {
func Benchmark_Set(b *testing.B) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := NewCache(ctx, time.Hour, false)
c := NewCache[string, string](ctx, time.Hour, false)

for n := 0; n < b.N; n++ {
err := c.Set(ctx, key, value)
Expand All @@ -489,7 +474,7 @@ func Benchmark_Set(b *testing.B) {
func Benchmark_Get(b *testing.B) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := NewCache(ctx, time.Hour, false)
c := NewCache[string, string](ctx, time.Hour, false)
c.Set(ctx, key, value)

for n := 0; n < b.N; n++ {
Expand Down
Loading

0 comments on commit 18e41c0

Please sign in to comment.