-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Stop policy eval when context is done
- Loading branch information
Showing
2 changed files
with
72 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package act | ||
|
||
import ( | ||
"context" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestPolicy_Eval(t *testing.T) { | ||
t.Run("it should eval program correctly", func(t *testing.T) { | ||
policy := MustNewPolicy("test", "1 + 1", map[string]any{}) | ||
resp, err := policy.Eval(context.Background(), Input{}) | ||
require.NoError(t, err) | ||
assert.Equal(t, 2, resp) | ||
}) | ||
|
||
t.Run("it should stop eval if context is timed out", func(t *testing.T) { | ||
policy := MustNewPolicy("test", "repeat(\"Hi\", 100000)", map[string]any{}) | ||
ctx, cancel := context.WithTimeout(context.Background(), 10) | ||
defer cancel() | ||
_, err := policy.Eval(ctx, Input{}) | ||
require.Error(t, err) | ||
}) | ||
|
||
t.Run("it should fail because of runtime error", func(t *testing.T) { | ||
policy := MustNewPolicy("test", `nonExistantVariable`, map[string]any{}) | ||
_, err := policy.Eval(context.Background(), Input{ | ||
Policy: map[string]any{"key": "value"}, | ||
}) | ||
require.ErrorIs(t, err, context.DeadlineExceeded) | ||
}) | ||
} | ||
|
||
func TestPolicy_MustCompile(t *testing.T) { | ||
t.Run("it should compile policy correctly", func(t *testing.T) { | ||
policy := Policy{ | ||
Name: "test", | ||
Policy: "1 + 1", | ||
Metadata: map[string]any{"log": "enabled"}, | ||
} | ||
require.NoError(t, policy.MustCompile()) | ||
}) | ||
|
||
t.Run("it should fail to compile policy if there is an error", func(t *testing.T) { | ||
policy := Policy{ | ||
Name: "test", | ||
Policy: "1 + \"Hi\"", | ||
Metadata: map[string]any{"log": "enabled"}, | ||
} | ||
require.Error(t, policy.MustCompile()) | ||
}) | ||
} |