Last active
June 4, 2026 23:14
-
-
Save DABH/91c0d0a45788b4e152d53ca175b92980 to your computer and use it in GitHub Desktop.
ADK-Go E2E Test of Custom Time and UUID Providers
This file contains hidden or 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
| package main | |
| import ( | |
| "context" | |
| "fmt" | |
| "iter" | |
| "time" | |
| "google.golang.org/genai" | |
| "google.golang.org/adk/agent" | |
| "google.golang.org/adk/agent/llmagent" | |
| "google.golang.org/adk/model" | |
| "google.golang.org/adk/platform" | |
| "google.golang.org/adk/runner" | |
| "google.golang.org/adk/session" | |
| ) | |
| func (fakeModel) Name() string { return "fake-model" } | |
| func (fakeModel) GenerateContent(ctx context.Context, req *model.LLMRequest, useStream bool) iter.Seq2[*model.LLMResponse, error] { | |
| return func(yield func(*model.LLMResponse, error) bool) { | |
| yield(&model.LLMResponse{Content: &genai.Content{ | |
| Role: "model", | |
| Parts: []*genai.Part{genai.NewPartFromText("hello from the agent")}, | |
| }}, nil) | |
| } | |
| } | |
| // Install fixed time + sequential UUID providers, as a workflow engine would on replay. | |
| func deterministicCtx(parent context.Context) context.Context { | |
| ids := 0 | |
| ctx := platform.WithUUIDProvider(parent, func() string { ids++; return fmt.Sprintf("det-id-%d", ids) }) | |
| secs := 0 | |
| return platform.WithTimeProvider(ctx, func() time.Time { | |
| secs++ | |
| return time.Date(2026, 1, 1, 0, 0, secs, 0, time.UTC) | |
| }) | |
| } | |
| func runOnce(label string) []string { | |
| a, _ := llmagent.New(llmagent.Config{Name: "tester", Model: fakeModel{}}) | |
| ss := session.InMemoryService() | |
| ss.Create(context.Background(), &session.CreateRequest{AppName: "app", UserID: "user", SessionID: "sess"}) | |
| r, _ := runner.New(runner.Config{Agent: a, SessionService: ss, AppName: "app"}) | |
| ctx := deterministicCtx(context.Background()) // <-- swap for context.Background() to see the control case | |
| msg := &genai.Content{Role: "user", Parts: []*genai.Part{genai.NewPartFromText("hi")}} | |
| var fp []string | |
| for ev, err := range r.Run(ctx, "user", "sess", msg, agent.RunConfig{StreamingMode: agent.StreamingModeSSE}) { | |
| if err != nil { | |
| panic(err) | |
| } | |
| line := fmt.Sprintf("id=%s ts=%s", ev.ID, ev.Timestamp.UTC().Format(time.RFC3339Nano)) | |
| fmt.Printf("[%s] %s\n", label, line) | |
| fp = append(fp, line) | |
| } | |
| return fp | |
| } | |
| func main() { | |
| first, second := runOnce("run-1"), runOnce("run-2") | |
| if fmt.Sprint(first) == fmt.Sprint(second) { | |
| fmt.Println("\nPASS: identical event IDs and timestamps across runs (replay-safe)") | |
| } else { | |
| fmt.Println("\nFAIL: runs diverged") | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment