package link import ( "testing" "time" amqp "github.com/rabbitmq/amqp091-go" ) // A machine asked to be five things becomes the last one: what is already waiting supersedes what // arrived first, and everything set aside is named so it can be reported. func TestWhatIsAlreadyWaitingSupersedesWhatArrivedFirst(t *testing.T) { deliveries := make(chan amqp.Delivery, 8) for _, id := range []string{"two", "three", "four"} { deliveries <- amqp.Delivery{Body: []byte(id)} } apply, superseded := newest(deliveries, amqp.Delivery{Body: []byte("one")}, 50*time.Millisecond) if string(apply.Body) != "four" { t.Fatalf("applied %q, not the newest", apply.Body) } if len(superseded) != 3 || string(superseded[0].Body) != "one" || string(superseded[2].Body) != "three" { t.Fatalf("set aside %d: %v", len(superseded), superseded) } } // One declaration with nothing behind it is applied as it always was, after the window. func TestALoneDeclarationIsAppliedAfterTheWindow(t *testing.T) { deliveries := make(chan amqp.Delivery, 1) began := time.Now() apply, superseded := newest(deliveries, amqp.Delivery{Body: []byte("only")}, 30*time.Millisecond) if string(apply.Body) != "only" || len(superseded) != 0 { t.Fatalf("got %q with %d set aside", apply.Body, len(superseded)) } if time.Since(began) < 30*time.Millisecond { t.Fatal("did not wait the window for a straggler") } } // A straggler within the window is taken; one after it is the next push. func TestAStragglerWithinTheWindowIsTaken(t *testing.T) { deliveries := make(chan amqp.Delivery, 2) go func() { time.Sleep(20 * time.Millisecond) deliveries <- amqp.Delivery{Body: []byte("late")} }() apply, superseded := newest(deliveries, amqp.Delivery{Body: []byte("first")}, 100*time.Millisecond) if string(apply.Body) != "late" || len(superseded) != 1 { t.Fatalf("got %q with %d set aside", apply.Body, len(superseded)) } }