Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable.Set Should Invalidated the Cached Value #73

Merged
merged 1 commit into from
Dec 22, 2022
Merged
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
3 changes: 3 additions & 0 deletions variable/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ func setFlushedValue(ctx context.Context, index uint32, value interface{}) error
if setter == nil {
return errors.New(errSetterNotFound + variable.Name())
}

// should invalidate the cached value before setting it to a new one
variableValue.Valid = false
return setter.Set(ctx, variableValue, value)
}
}
Expand Down
29 changes: 29 additions & 0 deletions variable/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,35 @@ func TestGetVariableValue_normal(t *testing.T) {

}

func TestSetShouldInvalidateCachedValue(t *testing.T) {
name := "SetShouldInvalidateCachedValue"
value := "1"

getter := func(ctx context.Context, v *IndexedValue, data interface{}) (string, error) {
return value, nil
}

setter := func(ctx context.Context, variableValue *IndexedValue, v string) error {
value = v
return nil
}

// register test variable
Register(NewStringVariable(name, nil, getter, setter, 0))

ctx := context.Background()
ctx = NewVariableContext(ctx)

s, _ := GetString(ctx, name) // make sure the value is cached
assert.Equal(t, s, "1")

_ = SetString(ctx, name, "2") // set to a new value, the cached value should be invalidated
assert.Equal(t, value, "2")

s, _ = GetString(ctx, name)
assert.Equal(t, s, "2")
}

func TestSetVariableValue_normal(t *testing.T) {
name := "ApiSet"
value := "Setter Value"
Expand Down