|
| 1 | +package ferret |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/MontFerret/ferret/v2/pkg/source" |
| 10 | +) |
| 11 | + |
| 12 | +func TestTimedWaitForRunsWithoutStdlib(t *testing.T) { |
| 13 | + engine := mustNewEngine(t, WithoutStdlib()) |
| 14 | + t.Cleanup(func() { _ = engine.Close() }) |
| 15 | + |
| 16 | + tests := []struct { |
| 17 | + name string |
| 18 | + query string |
| 19 | + want string |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "duration timeout", |
| 23 | + query: `LET ready = false RETURN WAITFOR ready TIMEOUT 1ms EVERY 0ms`, |
| 24 | + want: "false", |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "computed timeout", |
| 28 | + query: `LET ready = false LET timeout = 0.5ms RETURN WAITFOR ready TIMEOUT timeout * 2 EVERY 0ms`, |
| 29 | + want: "false", |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "timeout fallback", |
| 33 | + query: `RETURN WAITFOR VALUE "ready" WHEN false TIMEOUT 1ms EVERY 0ms ON TIMEOUT RETURN "timeout"`, |
| 34 | + want: `"timeout"`, |
| 35 | + }, |
| 36 | + } |
| 37 | + |
| 38 | + for _, tc := range tests { |
| 39 | + t.Run(tc.name, func(t *testing.T) { |
| 40 | + output, err := engine.Run(context.Background(), source.NewAnonymous(tc.query)) |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("run failed: %v", err) |
| 43 | + } |
| 44 | + if got := string(output.Content); got != tc.want { |
| 45 | + t.Fatalf("unexpected output: got %q, want %q", got, tc.want) |
| 46 | + } |
| 47 | + }) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestTimedWaitForWithoutStdlibHonorsCancellation(t *testing.T) { |
| 52 | + engine := mustNewEngine(t, WithoutStdlib()) |
| 53 | + t.Cleanup(func() { _ = engine.Close() }) |
| 54 | + |
| 55 | + ctx, cancel := context.WithCancel(context.Background()) |
| 56 | + cancel() |
| 57 | + |
| 58 | + _, err := engine.Run( |
| 59 | + ctx, |
| 60 | + source.NewAnonymous(`LET ready = false RETURN WAITFOR ready TIMEOUT 10s EVERY 10s`), |
| 61 | + ) |
| 62 | + if !errors.Is(err, context.Canceled) { |
| 63 | + t.Fatalf("expected cancellation, got %v", err) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestTimedWaitForWithoutStdlibSupportsRepeatedSessionRuns(t *testing.T) { |
| 68 | + engine := mustNewEngine(t, WithoutStdlib()) |
| 69 | + t.Cleanup(func() { _ = engine.Close() }) |
| 70 | + |
| 71 | + plan := mustCompilePlan(t, engine, `LET ready = false RETURN WAITFOR ready TIMEOUT 0.5ms EVERY 0ms`) |
| 72 | + t.Cleanup(func() { _ = plan.Close() }) |
| 73 | + |
| 74 | + session := mustNewSession(t, plan) |
| 75 | + t.Cleanup(func() { _ = session.Close() }) |
| 76 | + |
| 77 | + for run := 0; run < 2; run++ { |
| 78 | + output, err := session.Run(context.Background()) |
| 79 | + if err != nil { |
| 80 | + t.Fatalf("run %d failed: %v", run+1, err) |
| 81 | + } |
| 82 | + if got := string(output.Content); got != "false" { |
| 83 | + t.Fatalf("run %d returned %q, want false", run+1, got) |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestExplicitNowStillRequiresStdlib(t *testing.T) { |
| 89 | + engine := mustNewEngine(t, WithoutStdlib()) |
| 90 | + t.Cleanup(func() { _ = engine.Close() }) |
| 91 | + |
| 92 | + _, err := engine.Run(context.Background(), source.NewAnonymous(`RETURN NOW()`)) |
| 93 | + if err == nil || !strings.Contains(err.Error(), "unresolved function") { |
| 94 | + t.Fatalf("expected unresolved NOW function, got %v", err) |
| 95 | + } |
| 96 | +} |
0 commit comments