-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: "all_teams" data resource causes 429 status code requests (#988)
* Fix: "all_teams" data resource causes 429 status code requests * added a note * linting fixes * Added memoize tests
- Loading branch information
1 parent
8a80a6a
commit b871818
Showing
6 changed files
with
112 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package client | ||
|
||
type memoizedResult[V any] struct { | ||
value V | ||
err error | ||
} | ||
|
||
func memoize[K comparable, V any](f func(K) (V, error)) func(K) (V, error) { | ||
cache := make(map[K]memoizedResult[V]) | ||
|
||
return func(key K) (V, error) { | ||
if res, ok := cache[key]; ok { | ||
return res.value, res.err | ||
} | ||
|
||
value, err := f(key) | ||
|
||
cache[key] = memoizedResult[V]{value: value, err: err} | ||
|
||
return value, err | ||
} | ||
} | ||
|
||
// MemoizeExported exports the memoize function for testing | ||
func MemoizeExported[K comparable, V any](f func(K) (V, error)) func(K) (V, error) { | ||
return memoize(f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package client_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/env0/terraform-provider-env0/client" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMemoize(t *testing.T) { | ||
t.Run("should cache successful results", func(t *testing.T) { | ||
callCount := 0 | ||
f := func(s string) ([]client.Team, error) { | ||
callCount++ | ||
|
||
return []client.Team{{Name: s}}, nil | ||
} | ||
|
||
memoized := client.MemoizeExported(f) | ||
|
||
// First call | ||
result1, err1 := memoized("") | ||
require.NoError(t, err1) | ||
assert.Len(t, result1, 1) | ||
assert.Equal(t, 1, callCount) | ||
|
||
// Second call with same input - should use cache | ||
result2, err2 := memoized("") | ||
require.NoError(t, err2) | ||
assert.Len(t, result2, 1) | ||
assert.Equal(t, 1, callCount) // Count shouldn't increase | ||
|
||
// Different input - should call function again | ||
result3, err3 := memoized("test") | ||
require.NoError(t, err3) | ||
assert.Len(t, result3, 1) | ||
assert.Equal(t, "test", result3[0].Name) | ||
assert.Equal(t, 2, callCount) | ||
}) | ||
|
||
t.Run("should cache errors", func(t *testing.T) { | ||
callCount := 0 | ||
expectedError := errors.New("test error") | ||
f := func(s string) ([]client.Team, error) { | ||
callCount++ | ||
|
||
return nil, expectedError | ||
} | ||
|
||
memoized := client.MemoizeExported(f) | ||
|
||
// First call | ||
result1, err1 := memoized("") | ||
require.Error(t, err1) | ||
assert.Equal(t, expectedError, err1) | ||
assert.Nil(t, result1) | ||
assert.Equal(t, 1, callCount) | ||
|
||
// Second call - should return cached error | ||
result2, err2 := memoized("") | ||
require.Error(t, err2) | ||
assert.Equal(t, expectedError, err2) | ||
assert.Nil(t, result2) | ||
assert.Equal(t, 1, callCount) // Count shouldn't increase | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters