Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
version: "2"
run:
tests: false
linters:
default: none
enable:
Expand Down Expand Up @@ -36,6 +34,15 @@ linters:
- common-false-positives
- legacy
- std-error-handling
rules:
- path: '_test\.go'
linters:
- errcheck
- goheader
- ineffassign
- lll
- staticcheck
- unused
formatters:
enable:
- gofmt
Expand Down
64 changes: 22 additions & 42 deletions pkg/api/api/save_api_key_last_used_at_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func TestCacheAPIKeyLastUsedAt(t *testing.T) {
name string
apikey *account.EnvironmentAPIKey
lastUsedAt int64
existingCache sync.Map
expectedCache sync.Map
setupCache func(m *sync.Map)
expectedCache map[string]apikeyLastUsedAt
}{
{
name: "new entry",
Expand All @@ -45,19 +45,14 @@ func TestCacheAPIKeyLastUsedAt(t *testing.T) {
},
},
lastUsedAt: 1000,
existingCache: func() sync.Map {
var m sync.Map
return m
}(),
expectedCache: func() sync.Map {
var m sync.Map
m.Store("key1", apikeyLastUsedAt{
setupCache: func(m *sync.Map) {},
expectedCache: map[string]apikeyLastUsedAt{
"key1": {
apiKeyID: "key1",
lastUsedAt: 1000,
environmentID: "env1",
})
return m
}(),
},
},
},
{
name: "update existing entry with higher lastUsedAt",
Expand All @@ -70,24 +65,20 @@ func TestCacheAPIKeyLastUsedAt(t *testing.T) {
},
},
lastUsedAt: 2000,
existingCache: func() sync.Map {
var m sync.Map
setupCache: func(m *sync.Map) {
m.Store("key1", apikeyLastUsedAt{
apiKeyID: "key1",
lastUsedAt: 1500,
environmentID: "env1",
})
return m
}(),
expectedCache: func() sync.Map {
var m sync.Map
m.Store("key1", apikeyLastUsedAt{
},
expectedCache: map[string]apikeyLastUsedAt{
"key1": {
apiKeyID: "key1",
lastUsedAt: 2000,
environmentID: "env1",
})
return m
}(),
},
},
},
{
name: "do not update existing entry with lower lastUsedAt",
Expand All @@ -100,46 +91,35 @@ func TestCacheAPIKeyLastUsedAt(t *testing.T) {
},
},
lastUsedAt: 1000,
existingCache: func() sync.Map {
var m sync.Map
setupCache: func(m *sync.Map) {
m.Store("key1", apikeyLastUsedAt{
apiKeyID: "key1",
lastUsedAt: 1500,
environmentID: "env1",
})
return m
}(),
expectedCache: func() sync.Map {
var m sync.Map
m.Store("key1", apikeyLastUsedAt{
},
expectedCache: map[string]apikeyLastUsedAt{
"key1": {
apiKeyID: "key1",
lastUsedAt: 1500,
environmentID: "env1",
})
return m
}(),
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
service := &grpcGatewayService{
apiKeyLastUsedInfoCacher: tt.existingCache,
}
service := &grpcGatewayService{}
tt.setupCache(&service.apiKeyLastUsedInfoCacher)
service.cacheAPIKeyLastUsedAt(tt.apikey, tt.lastUsedAt)

listExpected := make(map[string]apikeyLastUsedAt)
tt.expectedCache.Range(func(key, value interface{}) bool {
listExpected[key.(string)] = value.(apikeyLastUsedAt)
return true
})

listActual := make(map[string]apikeyLastUsedAt)
service.apiKeyLastUsedInfoCacher.Range(func(key, value interface{}) bool {
listActual[key.(string)] = value.(apikeyLastUsedAt)
return true
})

assert.Equal(t, listExpected, listActual)
assert.Equal(t, tt.expectedCache, listActual)
})
}
}
Loading