(t *testing.T)
| 1505 | } |
| 1506 | |
| 1507 | func TestZoneAwareContextTracker(t *testing.T) { |
| 1508 | instance1 := InstanceDesc{Addr: "127.0.0.1", Zone: "zone-a"} |
| 1509 | instance2 := InstanceDesc{Addr: "127.0.0.2", Zone: "zone-a"} |
| 1510 | instance3 := InstanceDesc{Addr: "127.0.0.3", Zone: "zone-b"} |
| 1511 | instance4 := InstanceDesc{Addr: "127.0.0.4", Zone: "zone-b"} |
| 1512 | instance5 := InstanceDesc{Addr: "127.0.0.5", Zone: "zone-c"} |
| 1513 | instance6 := InstanceDesc{Addr: "127.0.0.6", Zone: "zone-c"} |
| 1514 | instances := []InstanceDesc{instance1, instance2, instance3, instance4, instance5, instance6} |
| 1515 | |
| 1516 | parentCtx := context.WithValue(context.Background(), testContextKey, "this-is-the-value-from-the-parent") |
| 1517 | tracker := newZoneAwareContextTracker(parentCtx, instances) |
| 1518 | |
| 1519 | instance1Ctx, _ := tracker.contextFor(&instances[0]) |
| 1520 | instance2Ctx, _ := tracker.contextFor(&instances[1]) |
| 1521 | instance3Ctx, instance3Cancel := tracker.contextFor(&instances[2]) |
| 1522 | instance4Ctx, _ := tracker.contextFor(&instances[3]) |
| 1523 | instance5Ctx, _ := tracker.contextFor(&instances[4]) |
| 1524 | instance6Ctx, _ := tracker.contextFor(&instances[5]) |
| 1525 | |
| 1526 | for _, ctx := range []context.Context{instance1Ctx, instance2Ctx, instance3Ctx, instance4Ctx, instance5Ctx, instance6Ctx} { |
| 1527 | require.Equal(t, "this-is-the-value-from-the-parent", ctx.Value(testContextKey), "context for instance should inherit from provided parent context") |
| 1528 | require.NoError(t, ctx.Err(), "context for instance should not be cancelled") |
| 1529 | } |
| 1530 | |
| 1531 | // Cancel a context for one instance using cancelContextFor and check that the other context in its zone is cancelled, but others are |
| 1532 | // unaffected. |
| 1533 | instance1Cause := cancellation.NewErrorf("instance 1 cancellation cause") |
| 1534 | tracker.cancelContextFor(&instance1, instance1Cause) |
| 1535 | require.Equal(t, context.Canceled, instance1Ctx.Err(), "instance context should be cancelled") |
| 1536 | require.Equal(t, context.Canceled, instance2Ctx.Err(), "context for instance in same zone as cancelled instance should also be cancelled") |
| 1537 | require.Equal(t, instance1Cause, context.Cause(instance1Ctx)) |
| 1538 | require.Equal(t, instance1Cause, context.Cause(instance2Ctx)) |
| 1539 | for _, ctx := range []context.Context{instance3Ctx, instance4Ctx, instance5Ctx, instance6Ctx} { |
| 1540 | require.NoError(t, ctx.Err(), "context for instance should not be cancelled after cancelling the context of another instance") |
| 1541 | } |
| 1542 | |
| 1543 | // Cancel a context for one instance using the cancellation function provided and check that the other context in its zone is NOT cancelled. |
| 1544 | instance3Cause := cancellation.NewErrorf("instance 3 cancellation cause") |
| 1545 | instance3Cancel(instance3Cause) |
| 1546 | for _, ctx := range []context.Context{instance4Ctx, instance5Ctx, instance6Ctx} { |
| 1547 | require.NoError(t, ctx.Err(), "context for instance should not be cancelled after cancelling the context of another instance") |
| 1548 | } |
| 1549 | |
| 1550 | remainingInstancesCause := cancellation.NewErrorf("remaining instances cancellation cause") |
| 1551 | tracker.cancelAllContexts(remainingInstancesCause) |
| 1552 | for _, ctx := range []context.Context{instance4Ctx, instance5Ctx, instance6Ctx} { |
| 1553 | require.Equal(t, context.Canceled, ctx.Err(), "context for instance should be cancelled after cancelling all contexts") |
| 1554 | require.Equal(t, remainingInstancesCause, context.Cause(ctx)) |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | func TestInflightInstanceTracker(t *testing.T) { |
| 1559 | sets := []ReplicationSet{ |
nothing calls this directly
no test coverage detected