Skip to content

Commit 8bdfdc6

Browse files
authored
Merge pull request #2217 from amaanx86/bugfix/persists-messages-flake
test: fix the flaky persists messages system test
2 parents 346a71c + 6bd64a8 commit 8bdfdc6

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

test/system/system_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,14 @@ CONSOLE_LOG=new`
269269
)
270270

271271
BeforeEach(func(ctx SpecContext) {
272-
cluster = newRabbitmqCluster(namespace, "persistence-rabbit")
273-
Expect(createRabbitmqCluster(ctx, rmqClusterClient, cluster)).To(Succeed())
272+
// The AfterEach wait can be cut short by the spec deadline, so retry the create too.
273+
Eventually(ctx, func() error {
274+
cluster = newRabbitmqCluster(namespace, "persistence-rabbit")
275+
return createRabbitmqCluster(ctx, rmqClusterClient, cluster)
276+
}).
277+
WithTimeout(clusterCreationTimeout).
278+
WithPolling(2 * time.Second).
279+
Should(Succeed())
274280

275281
waitForRabbitmqRunning(cluster)
276282

@@ -283,7 +289,9 @@ CONSOLE_LOG=new`
283289
})
284290

285291
AfterEach(func(ctx SpecContext) {
286-
Expect(rmqClusterClient.Delete(ctx, cluster)).To(Succeed())
292+
// A FlakeAttempts retry recreates this name milliseconds after we return, so block
293+
// until the cluster is actually gone.
294+
deleteRabbitmqClusterAndWait(ctx, rmqClusterClient, cluster)
287295
})
288296

289297
It("persists messages", FlakeAttempts(3), func(ctx SpecContext) {
@@ -304,7 +312,8 @@ CONSOLE_LOG=new`
304312
Expect(err).NotTo(HaveOccurred())
305313
Expect(message.Payload).To(Equal("hello"))
306314
})
307-
}, SpecTimeout(time.Minute*3))
315+
// The deadline covers the whole attempt, teardown included, and resets on each retry.
316+
}, SpecTimeout(time.Minute*5))
308317
})
309318

310319
Context("Persistence expansion", Label("persistence_expansion"), func() {

test/system/utils_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import (
5050
"github.com/rabbitmq/rabbitmq-stream-go-client/pkg/message"
5151
"github.com/rabbitmq/rabbitmq-stream-go-client/pkg/stream"
5252
corev1 "k8s.io/api/core/v1"
53+
k8serrors "k8s.io/apimachinery/pkg/api/errors"
5354
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
5455
"k8s.io/apimachinery/pkg/types"
5556
"k8s.io/client-go/kubernetes"
@@ -62,6 +63,11 @@ import (
6263
const podCreationTimeout = 10 * time.Minute
6364
const portReadinessTimeout = 1 * time.Minute
6465
const k8sQueryTimeout = 1 * time.Minute
66+
const clusterDeletionTimeout = 90 * time.Second
67+
68+
// A create is rejected while a cluster of the same name is still terminating, so the retry needs
69+
// to cover a deletion.
70+
const clusterCreationTimeout = clusterDeletionTimeout
6571

6672
type featureFlag struct {
6773
Name string
@@ -507,6 +513,32 @@ func createRabbitmqCluster(ctx context.Context, client client.Client, rabbitmqCl
507513
return client.Create(ctx, rabbitmqCluster)
508514
}
509515

516+
// deleteRabbitmqClusterAndWait deletes a RabbitmqCluster and waits for it to disappear. Delete only
517+
// marks it: the operator's finalizer keeps the object in Terminating for a few seconds, and
518+
// recreating the same name before then fails with "object is being deleted ... already exists".
519+
func deleteRabbitmqClusterAndWait(ctx context.Context, c client.Client, cluster *rabbitmqv1beta1.RabbitmqCluster) {
520+
GinkgoHelper()
521+
522+
Expect(client.IgnoreNotFound(c.Delete(ctx, cluster))).To(Succeed())
523+
524+
key := types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}
525+
Eventually(ctx, func() error {
526+
var current rabbitmqv1beta1.RabbitmqCluster
527+
err := c.Get(ctx, key, &current)
528+
if k8serrors.IsNotFound(err) {
529+
return nil
530+
}
531+
if err != nil {
532+
return err
533+
}
534+
return fmt.Errorf("RabbitmqCluster %s is still present (deletionTimestamp: %v, finalizers: %v)",
535+
key, current.DeletionTimestamp, current.Finalizers)
536+
}).
537+
WithTimeout(clusterDeletionTimeout).
538+
WithPolling(2 * time.Second).
539+
Should(Succeed())
540+
}
541+
510542
func statefulSetPodName(cluster *rabbitmqv1beta1.RabbitmqCluster, index int) string {
511543
return cluster.ChildResourceName(strings.Join([]string{"server", strconv.Itoa(index)}, "-"))
512544
}

0 commit comments

Comments
 (0)