|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package cloudwatch_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + cw "github.com/stablekernel/crucible/sink/cloudwatch" |
| 11 | +) |
| 12 | + |
| 13 | +// TestPutLogEventAt_StampsSuppliedTime verifies the clock-injectable variant |
| 14 | +// stamps the event with the caller-supplied time rather than the wall clock, so |
| 15 | +// the emitted timestamp is deterministic. |
| 16 | +func TestPutLogEventAt_StampsSuppliedTime(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + at := time.Unix(1700000000, 0) // a fixed, known instant |
| 20 | + c := &fakeClient{} |
| 21 | + op := cw.PutLogEventAt("/app/events", "app-stream", "deterministic", at) |
| 22 | + if err := op.Apply(context.Background(), c); err != nil { |
| 23 | + t.Fatalf("Apply() error = %v", err) |
| 24 | + } |
| 25 | + if len(c.calls) != 1 { |
| 26 | + t.Fatalf("PutLogEvents call count = %d, want 1", len(c.calls)) |
| 27 | + } |
| 28 | + events := c.calls[0].LogEvents |
| 29 | + if len(events) != 1 { |
| 30 | + t.Fatalf("events count = %d, want 1", len(events)) |
| 31 | + } |
| 32 | + if events[0].Timestamp == nil { |
| 33 | + t.Fatal("event timestamp is nil") |
| 34 | + } |
| 35 | + if got, want := *events[0].Timestamp, at.UnixMilli(); got != want { |
| 36 | + t.Fatalf("timestamp = %d, want %d (the supplied time in ms)", got, want) |
| 37 | + } |
| 38 | + if events[0].Message == nil || *events[0].Message != "deterministic" { |
| 39 | + t.Errorf("message = %v, want %q", events[0].Message, "deterministic") |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// TestPutLogEvent_DelegatesToPutLogEventAt verifies the convenience wrapper |
| 44 | +// still emits exactly one event to the named group and stream. |
| 45 | +func TestPutLogEvent_DelegatesToPutLogEventAt(t *testing.T) { |
| 46 | + t.Parallel() |
| 47 | + |
| 48 | + c := &fakeClient{} |
| 49 | + op := cw.PutLogEvent("/app/events", "app-stream", "now") |
| 50 | + if err := op.Apply(context.Background(), c); err != nil { |
| 51 | + t.Fatalf("Apply() error = %v", err) |
| 52 | + } |
| 53 | + if len(c.calls) != 1 || len(c.calls[0].LogEvents) != 1 { |
| 54 | + t.Fatalf("calls = %+v, want one call with one event", c.calls) |
| 55 | + } |
| 56 | + if c.calls[0].LogEvents[0].Timestamp == nil { |
| 57 | + t.Error("PutLogEvent should stamp a timestamp") |
| 58 | + } |
| 59 | +} |
0 commit comments