| 1 | package activator |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/carlosframework/platform/internal/registry" |
| 8 | ) |
| 9 | |
| 10 | // seedPlacingBackend is a fakeBackend that records SeedPlaced calls. |
| 11 | type seedPlacingBackend struct { |
| 12 | *fakeBackend |
| 13 | placed []string |
| 14 | rep Rep |
| 15 | } |
| 16 | |
| 17 | func (b *seedPlacingBackend) SeedPlaced(_ context.Context, rt registry.Route, rep Rep) error { |
| 18 | b.placed = append(b.placed, rt.Host) |
| 19 | b.rep = rep |
| 20 | return nil |
| 21 | } |
| 22 | |
| 23 | type noSyncRep struct{} |
| 24 | |
| 25 | func (noSyncRep) Err() error { return nil } |
| 26 | func (noSyncRep) Close(context.Context) error { return nil } |
| 27 | |
| 28 | // SplitBackend.SeedPlaced dispatches on Backing: an exec route reaches the |
| 29 | // exec backend's SeedPlaced with the same Rep; a unit route is never |
| 30 | // forwarded and answers nil. |
| 31 | func TestSplitBackendSeedPlacedDispatchesByBacking(t *testing.T) { |
| 32 | eb := &seedPlacingBackend{fakeBackend: &fakeBackend{}} |
| 33 | sb := &SplitBackend{Exec: eb, Unit: &UnitBackend{OptDir: t.TempDir()}} |
| 34 | ctx := context.Background() |
| 35 | rep := noSyncRep{} |
| 36 | |
| 37 | if err := sb.SeedPlaced(ctx, testUnitRoute("unit.example.com"), rep); err != nil { |
| 38 | t.Fatalf("unit route: %v", err) |
| 39 | } |
| 40 | if len(eb.placed) != 0 { |
| 41 | t.Fatalf("a unit route reached the exec backend: %q", eb.placed) |
| 42 | } |
| 43 | if err := sb.SeedPlaced(ctx, testRoute("shop-1.example.com"), rep); err != nil { |
| 44 | t.Fatalf("exec route: %v", err) |
| 45 | } |
| 46 | if len(eb.placed) != 1 || eb.placed[0] != "shop-1.example.com" || eb.rep != Rep(rep) { |
| 47 | t.Fatalf("exec route forwarded %q with rep %v", eb.placed, eb.rep) |
| 48 | } |
| 49 | } |
| 50 | |