|
| 1 | +package events |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/redis/go-redis/v9" |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | + "github.com/twmb/franz-go/pkg/kgo" |
| 8 | + "github.com/wundergraph/cosmo/router-tests/testenv" |
| 9 | + "net/url" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +const waitTimeout = time.Second * 30 |
| 15 | + |
| 16 | +func ProduceKafkaMessage(t *testing.T, xEnv *testenv.Environment, topicName string, message string) { |
| 17 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 18 | + defer cancel() |
| 19 | + |
| 20 | + pErrCh := make(chan error) |
| 21 | + |
| 22 | + xEnv.KafkaClient.Produce(ctx, &kgo.Record{ |
| 23 | + Topic: xEnv.GetPubSubName(topicName), |
| 24 | + Value: []byte(message), |
| 25 | + }, func(_ *kgo.Record, err error) { |
| 26 | + pErrCh <- err |
| 27 | + }) |
| 28 | + |
| 29 | + testenv.AwaitChannelWithT(t, waitTimeout, pErrCh, func(t *testing.T, pErr error) { |
| 30 | + require.NoError(t, pErr) |
| 31 | + }) |
| 32 | + |
| 33 | + fErr := xEnv.KafkaClient.Flush(ctx) |
| 34 | + require.NoError(t, fErr) |
| 35 | +} |
| 36 | + |
| 37 | +func EnsureTopicExists(t *testing.T, xEnv *testenv.Environment, topics ...string) { |
| 38 | + // Delete topic for idempotency |
| 39 | + deleteCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 40 | + defer cancel() |
| 41 | + prefixedTopics := make([]string, 0, len(topics)) |
| 42 | + for _, topic := range topics { |
| 43 | + prefixedTopics = append(prefixedTopics, xEnv.GetPubSubName(topic)) |
| 44 | + } |
| 45 | + |
| 46 | + _, err := xEnv.KafkaAdminClient.DeleteTopics(deleteCtx, prefixedTopics...) |
| 47 | + require.NoError(t, err) |
| 48 | + |
| 49 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 50 | + defer cancel() |
| 51 | + |
| 52 | + _, err = xEnv.KafkaAdminClient.CreateTopics(ctx, 1, 1, nil, prefixedTopics...) |
| 53 | + require.NoError(t, err) |
| 54 | +} |
| 55 | + |
| 56 | +func ProduceRedisMessage(t *testing.T, xEnv *testenv.Environment, topicName string, message string) { |
| 57 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 58 | + defer cancel() |
| 59 | + |
| 60 | + parsedURL, err := url.Parse(xEnv.RedisHosts[0]) |
| 61 | + if err != nil { |
| 62 | + t.Fatalf("Failed to parse Redis URL: %v", err) |
| 63 | + } |
| 64 | + var redisConn redis.UniversalClient |
| 65 | + if !xEnv.RedisWithClusterMode { |
| 66 | + redisConn = redis.NewClient(&redis.Options{ |
| 67 | + Addr: parsedURL.Host, |
| 68 | + }) |
| 69 | + } else { |
| 70 | + redisConn = redis.NewClusterClient(&redis.ClusterOptions{ |
| 71 | + Addrs: []string{parsedURL.Host}, |
| 72 | + }) |
| 73 | + } |
| 74 | + |
| 75 | + defer func() { |
| 76 | + _ = redisConn.Close() |
| 77 | + }() |
| 78 | + |
| 79 | + intCmd := redisConn.Publish(ctx, xEnv.GetPubSubName(topicName), message) |
| 80 | + require.NoError(t, intCmd.Err()) |
| 81 | +} |
0 commit comments