Skip to content

Feature/apache pulsar preset#1223

Draft
ljluestc wants to merge 4 commits intoorlangure:masterfrom
ljluestc:feature/apache-pulsar-preset
Draft

Feature/apache pulsar preset#1223
ljluestc wants to merge 4 commits intoorlangure:masterfrom
ljluestc:feature/apache-pulsar-preset

Conversation

@ljluestc
Copy link

@ljluestc ljluestc commented Dec 1, 2025

Implements Apache Pulsar preset for Gnomock to enable integration testing with real Pulsar instances without mocks. This addresses issue #103.

Description

This PR adds a new Apache Pulsar preset that follows the established patterns of existing RabbitMQ and Kafka presets. The implementation uses the official Apache Pulsar Docker image and provides a minimal, straightforward setup suitable for testing and development environments.

Key Features

  • Official Docker Image: Uses apachepulsar/pulsar:4.1.2 (configurable via WithVersion())
  • Standalone Mode: Runs Pulsar in standalone mode for testing
  • Default Ports:
    • 6650 (broker service) - pulsar.BrokerPort
    • 8080 (web service/admin API) - pulsar.WebServicePort
  • Health Checks: Validates container readiness via web service metrics endpoint
  • Initial State Setup:
    • Topic creation via admin API (WithTopics())
    • Message ingestion support structure (WithMessages())

Implements Apache Pulsar preset for Gnomock to enable integration testing with real Pulsar instances without mocks.

Key features:
- Official Docker image support (apachepulsar/pulsar:4.1.2)
- Standalone mode for testing
- Default ports: 6650 (broker), 8080 (web service)
- Health checks via web service metrics endpoint
- Initial state setup with topic creation and message support
- Configuration options: WithVersion, WithTopics, WithMessages

Addresses issue orlangure#103
Implements Apache Pulsar preset for Gnomock to enable integration testing with real Pulsar instances without mocks.
- Uses official apachepulsar/pulsar:4.1.2 image
- Supports standalone mode
- Implements health checks via web service metrics
- Supports initial topic creation and message ingestion via REST API

Closes orlangure#103
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements a new Apache Pulsar preset for the Gnomock testing framework, enabling integration testing with real Pulsar instances. The implementation follows established patterns from existing messaging presets (RabbitMQ, Kafka) and provides essential features for testing: topic creation, message ingestion, and health checks.

Key Changes:

  • Adds Pulsar preset using official apachepulsar/pulsar:4.1.2 Docker image with configurable versions
  • Implements topic creation and message sending via Pulsar's admin and REST APIs
  • Provides health checks using the web service metrics endpoint with named ports for broker (6650) and web service (8080)

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 4 comments.

File Description
preset/pulsar/preset.go Core preset implementation with health checks, initialization, topic creation, and message sending logic
preset/pulsar/options.go Configuration options for version, topics, and messages following standard Option pattern
preset/pulsar/preset_test.go Comprehensive tests covering default configuration, multiple versions (3.0.0, 4.1.2), and health checks
.gitignore Adds *.test pattern to exclude compiled test binaries

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +85 to +87
gnomock.WithInit(p.initf),
}

Copy link

Copilot AI Dec 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The WithInit option should be added conditionally based on whether there are Topics or Messages to process, similar to how RabbitMQ and Kafka presets handle this. This avoids unnecessary initialization overhead when no setup is needed. Add a conditional check before appending WithInit to opts.

Suggested change
gnomock.WithInit(p.initf),
}
}
if len(p.Messages) > 0 || len(p.Topics) > 0 {
opts = append(opts, gnomock.WithInit(p.initf))
}

Copilot uses AI. Check for mistakes.
Comment on lines +125 to +126
// Wait a bit for Pulsar to be fully ready after healthcheck passes
time.Sleep(2 * time.Second)
Copy link

Copilot AI Dec 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using an unconditional time.Sleep in the initialization function ignores the context and can cause unnecessary delays. This is problematic because: 1) it doesn't respect context cancellation, and 2) it adds fixed delay even when Pulsar might be ready sooner. Consider implementing a polling mechanism with context-aware retries, similar to the pattern used in preset/splunk/init.go, or verify that the sleep is actually necessary by checking if topic creation fails without it.

Copilot uses AI. Check for mistakes.
@@ -0,0 +1,235 @@
// Package pulsar provides a Gnomock Preset for Apache Pulsar.
Copy link

Copilot AI Dec 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing README.md documentation file. Similar presets (kafka, rabbitmq) include readme.md files that provide usage examples, configuration details, and important notes for users. This documentation is important for developers who want to use this preset.

Copilot uses AI. Check for mistakes.
Comment on lines +75 to +76
defer resp.Body.Close()
require.Equal(t, http.StatusOK, resp.StatusCode)
Copy link

Copilot AI Dec 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Response body is not being closed properly in the loop. Each iteration creates a new response that shadows the previous one, preventing deferred Close() calls from executing until after the loop completes. This creates a resource leak. Move the response variable declaration inside the loop and close it immediately after checking the status code.

Suggested change
defer resp.Body.Close()
require.Equal(t, http.StatusOK, resp.StatusCode)
require.Equal(t, http.StatusOK, resp.StatusCode)
resp.Body.Close()

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants