Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@Ren0503
Copy link
Contributor

@Ren0503 Ren0503 commented Sep 18, 2025

No description provided.

@Ren0503 Ren0503 added this to the Microservices v1.2.0 milestone Sep 18, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Sep 18, 2025

Summary by CodeRabbit

  • Refactor
    • Simplified TCP service initialization to accept a constructed module instance rather than a factory, reducing coupling and clarifying setup. This is a breaking change; update your initialization accordingly.
  • Tests
    • Updated test suites to use the new initialization approach across context, exception, guard, handler, middleware, pipe, and error scenarios for consistency.

Walkthrough

The TCP server constructor now accepts a core.Module instance instead of a factory/function. All affected tests were updated to pass a concrete module via appModule() or serverModule(); one test passes nil. Imports were adjusted accordingly. No other functional paths were modified.

Changes

Cohort / File(s) Summary of Changes
TCP constructor signature change
microservices/tcp/server.go
Changed tcp.New signature from func New(module core.ModuleParam, opts ...Options) to func New(module core.Module, opts ...Options) and updated internal assignment to use the provided instance directly.
Tests updated to pass Module instances
microservices/ctx_test.go, microservices/exception_test.go, microservices/guard_test.go, microservices/handler_test.go, microservices/middleware_test.go, microservices/pipe_test.go, microservices/tcp/error_test.go
Replaced passing factory functions with concrete module instances by invoking factory functions: appModuleappModule(), serverModuleserverModule(). In ctx_test.go, first arg changed to nil and removed dependency on v2/core. All keep existing options (e.g., Addr).

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Test as Test
  participant TCP as tcp.New
  participant Server as TCPServer

  Test->>TCP: New(moduleInstance, opts)
  note over Test,TCP: Module is already constructed (no factory)
  TCP->>Server: Initialize with provided module
  Server-->>Test: Service instance
Loading
sequenceDiagram
  autonumber
  actor Test as Test (Old)
  participant TCP as tcp.New (Old)
  participant Factory as moduleFactory()
  participant Server as TCPServer

  Test->>TCP: New(moduleFactory, opts)
  TCP->>Factory: Invoke to get module
  Factory-->>TCP: core.Module
  TCP->>Server: Initialize with module
  Server-->>Test: Service instance
  note over diagram: Old flow (factory) replaced by direct module instance
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A bunny taps the build with glee,
No factories—just modules, whee!
The sockets hum, the tests all pass,
We hop from function to instanced class.
Ears up high, we ship this brew—
New() is simpler, carrot-true. 🥕🐇

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Description Check ⚠️ Warning No pull request description was provided, so there is no authored summary of motivation, scope, or rationale for the changes; while the title and raw summaries make the change apparent, the absence of a description fails the lenient description check because it provides nothing for reviewers to assess. Please add a short description explaining why the constructor signature was changed, note any backward-compatibility or migration implications, list the key files modified (for example microservices/tcp/server.go and the updated tests), and include any testing or rollout notes reviewers should know.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (1 passed)
Check name Status Explanation
Title Check ✅ Passed The title accurately reflects the primary change in the diff: switching the constructor/option to accept a concrete module instead of a module-factory/ModuleParam (as seen in microservices/tcp/server.go and related tests), so it summarizes the main change; however it contains typos ("construstor" → "constructor" and "moduleparam" → "ModuleParam") and could be phrased more clearly.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/ren/332-change-option-constructor

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link

codecov bot commented Sep 18, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (3)
microservices/tcp/server.go (2)

62-70: Panic early if Module is nil.

Avoid a nil-interface method call in Listen() when svc.Module is nil.

Apply:

 func (svc *Server) Listen() {
+  if svc.Module == nil {
+    panic("module is nil; pass a module to tcp.New or call (*Server).Create before Listen")
+  }
   listener, err := net.Listen("tcp", svc.Addr)
   if err != nil {
     panic(err)
   }

3-15: Do not share the same net.Listener between http.Serve and manual Accept.

http.Serve(listener, nil) races with your own Accept() loop on the same listener; connections will be unpredictably consumed by one or the other.

Apply:

 import (
   "bufio"
   "fmt"
   "net"
-  "net/http"
   "reflect"
   "slices"
   "strings"
@@
-  go http.Serve(listener, nil)

Also applies to: 72-72

microservices/ctx_test.go (1)

66-72: Fix assertions: using payload instead of payload2 (and wrong expected values).

The second parse checks payload instead of payload2, and expected values should match the second message.

Apply:

- err = ctx2.PayloadParser(&payload2)
- require.Nil(t, err)
- require.Equal(t, "[email protected]", payload.Email)
- require.Equal(t, "12345678@Tc", payload.Password)
+ err = ctx2.PayloadParser(&payload2)
+ require.Nil(t, err)
+ require.Equal(t, "[email protected]", payload2.Email)
+ require.Equal(t, "12345678", payload2.Password)
🧹 Nitpick comments (4)
microservices/tcp/server.go (2)

23-26: Constructor change is fine; consider a compatibility helper and nil-module guard.

  • Add a helper to ease migration from factory style: NewFromFactory(fn core.ModuleParam, opts ...Options) microservices.Service { return New(fn(), opts...) }.
  • Also, if module can be nil in some call sites/tests, guard in Listen() to fail fast.

Apply:

 func New(module core.Module, opts ...Options) microservices.Service {
   svc := &Server{
-    Module: module,
+    Module: module,
     config: microservices.DefaultConfig(),
   }
   ...
 }
+
+// NewFromFactory provides a transitional API for callers still using a factory.
+func NewFromFactory(factory core.ModuleParam, opts ...Options) microservices.Service {
+	return New(factory(), opts...)
+}

117-123: Wildcard match should check suffix, not “contains any”.

strings.ContainsAny(msg.Event, "*") matches any asterisk anywhere, but you then trim only a trailing *. Use HasSuffix for intent clarity.

Apply:

- } else if strings.ContainsAny(msg.Event, "*") {
+ } else if strings.HasSuffix(msg.Event, "*") {
   prefix := strings.TrimSuffix(msg.Event, "*")
microservices/handler_test.go (1)

50-51: Avoid fixed sleeps; prefer readiness probe.

time.Sleep(1000 * time.Millisecond) makes the test slow and flaky across environments. Poll for port readiness with a timeout instead.

I can provide a tiny helper to wait for addr to accept connections if useful.

microservices/ctx_test.go (1)

18-18: Don’t pass a nil Module; use tcp.Open for config-only service.

tcp.New(nil, ...) risks nil-method calls if code evolves. Use tcp.Open(tcp.Options{...}) or supply a minimal module.

Apply:

- svc := tcp.New(nil, tcp.Options{Addr: "127.0.0.1:5173"})
+ svc := tcp.Open(tcp.Options{Addr: "127.0.0.1:5173"})
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 52d1be9 and 6b54f01.

📒 Files selected for processing (8)
  • microservices/ctx_test.go (1 hunks)
  • microservices/exception_test.go (1 hunks)
  • microservices/guard_test.go (1 hunks)
  • microservices/handler_test.go (1 hunks)
  • microservices/middleware_test.go (1 hunks)
  • microservices/pipe_test.go (1 hunks)
  • microservices/tcp/error_test.go (3 hunks)
  • microservices/tcp/server.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (8)
microservices/guard_test.go (3)
microservices/tcp/server.go (1)
  • New (23-39)
microservices/kafka/connect.go (2)
  • New (70-90)
  • Options (13-17)
microservices/nats/connect.go (2)
  • New (82-102)
  • Options (15-19)
microservices/tcp/error_test.go (2)
microservices/tcp/server.go (1)
  • New (23-39)
microservices/tcp/client.go (1)
  • Options (10-14)
microservices/tcp/server.go (5)
microservices/mqtt/connect.go (2)
  • New (96-113)
  • Options (21-24)
microservices/kafka/connect.go (2)
  • New (70-90)
  • Options (13-17)
microservices/nats/connect.go (2)
  • New (82-102)
  • Options (15-19)
microservices/redis/connect.go (2)
  • New (90-112)
  • Options (15-18)
microservices/amqlib/connect.go (2)
  • New (166-187)
  • Options (14-17)
microservices/exception_test.go (2)
microservices/tcp/server.go (1)
  • New (23-39)
microservices/tcp/client.go (1)
  • Options (10-14)
microservices/ctx_test.go (2)
microservices/tcp/server.go (1)
  • New (23-39)
microservices/tcp/client.go (1)
  • Options (10-14)
microservices/middleware_test.go (1)
microservices/tcp/server.go (1)
  • New (23-39)
microservices/pipe_test.go (2)
microservices/tcp/server.go (1)
  • New (23-39)
microservices/kafka/connect.go (2)
  • New (70-90)
  • Options (13-17)
microservices/handler_test.go (1)
microservices/tcp/server.go (1)
  • New (23-39)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Analyze (go)
🔇 Additional comments (8)
microservices/pipe_test.go (1)

77-82: Updated tcp.New usage looks correct.

Passing appModule() (a core.Module) matches the new constructor signature. No further action here.

microservices/guard_test.go (1)

41-43: LGTM on switching to module value.

Invocation now matches tcp.New(core.Module, ...Options).

microservices/exception_test.go (1)

38-40: LGTM on constructor change.

appModule() passed correctly to the new tcp.New signature.

microservices/handler_test.go (1)

94-96: LGTM on updated call site.

Passing appModule() aligns with the new API.

microservices/middleware_test.go (1)

46-48: LGTM on switching to module instance.

Constructor usage matches new signature.

microservices/tcp/error_test.go (3)

43-45: LGTM: updated to pass serverModule().

Matches new tcp.New signature.


103-105: LGTM: constructor updated in first panic case.


114-116: LGTM: constructor updated in second panic case.

@Ren0503 Ren0503 linked an issue Sep 18, 2025 that may be closed by this pull request
@Ren0503 Ren0503 merged commit 26eab47 into master Sep 18, 2025
8 checks passed
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.

Change option constructor service

2 participants