Skip to content

Add tests - #41

Closed
xuanyanwow wants to merge 43 commits into
mainfrom
10969/tests
Closed

Add tests #41
xuanyanwow wants to merge 43 commits into
mainfrom
10969/tests

Conversation

@xuanyanwow

@xuanyanwow xuanyanwow commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • New Features

    • Added multiplexed RPC transport with dedicated packer and data formatter.
    • Introduced metadata registration system for managing service configurations.
    • Added comprehensive helper functions library (retry, error handling, value utilities).
    • Added support utilities for array and string operations.
    • Improved load balancer error handling with dedicated exception type.
  • Documentation

    • Updated README with improved usage examples and enhanced coroutine support documentation.
  • Chores

    • Updated project branding and dependencies.
    • Improved development tooling configuration for testing.

huangdijia and others added 30 commits July 20, 2022 17:14
Co-Authored-By: hzh <hzh@addcn.com>
Co-Authored-By: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
Co-Authored-By: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
Co-Authored-By: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
* Split Arr and Str

Co-Authored-By: Deeka Wong <8337659+huangdijia@users.noreply.github.com>

* feat: Update path generation to handle namespaces with backslashes

* feat: Fix backslash handling in path generation

---------

Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
* Optimized timeout of transporter

Co-Authored-By: Deeka Wong <8337659+huangdijia@users.noreply.github.com>

* chore: Remove unused timeout property in StreamSocketTransporter

* feat: Update GuzzleHttpTransporter timeout handling

The code changes in `GuzzleHttpTransporter.php` update the timeout handling in the constructor. The `timeout` value is now retrieved from the `$config` array and assigned to the transporter's `timeout` property. This change ensures that the timeout value is correctly set when creating a new instance of `GuzzleHttpTransporter`.

Based on recent user commits and repository commits, it seems that there have been optimizations and fixes related to transporters and helper functions. However, these changes are not directly related to the current code changes in `GuzzleHttpTransporter.php`.

Please note that the suggested commit message follows the format "feat: <summary of changes>". Feel free to modify it according to your repository's commit message conventions.

* feat: Update GuzzleHttpTransporter timeout handling

* feat: Update GuzzleHttpTransporter timeout handling

* feat: Update GuzzleHttpTransporter timeout handling

---------

Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
* Optimize config of GuzzleHttpClient

* feat: Update GuzzleHttpTransporter configuration handling

The code changes in `GuzzleHttpTransporter.php` update the configuration handling in the constructor. The `$config` array is now merged with a default configuration array using `array_replace()`. This change ensures that the transporter has the correct configuration values when creating a new instance of `GuzzleHttpTransporter`.

Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>

---------

Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
The code changes in `GuzzleHttpTransporter.php` update the configuration handling in the constructor. The `$config` array is now merged with a default configuration array using `array_replace()`. This change ensures that the transporter has the correct configuration values when creating a new instance of `GuzzleHttpTransporter`.
…, setPathGenerator, setRegistry, setTries, and setTimeout methods in Metadata class

Refactor the setProtocol, setTransporter, setPacker, setDataFormatter, setPathGenerator, setRegistry, setTries, and setTimeout methods in the Metadata class to include a return type annotation of $this. This change improves the code readability and maintainability by explicitly indicating that these methods return an instance of the Metadata class.

Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
* Adds MetadataManager

* feat: Refactor MetadataManager to use static methods for registration and retrieval

The code changes in `MetadataManager.php` remove the unnecessary comments and update the `register` and `get` methods to be static. This refactor improves the code readability and maintainability by clearly indicating that these methods can be accessed directly on the class without needing an instance.

Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>

* feat: Update MetadataManager to use static methods for registration and retrieval

* feat: Update ClientTest to use service parameter in ClientFactory

The code changes in `ClientTest.php` update the `ClientFactory::create()` method to pass the `this->service` parameter as the first argument instead of `'test'`. This change ensures that the correct service is used when creating a new instance of the client.

---------

Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
Refactor the return type annotation in the `StreamSocketTransporter.php` file to use the `array{string, int}` type instead of `(int|string)[]`. This change improves the code readability and maintainability by explicitly indicating the expected return type.

Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
huangdijia and others added 13 commits August 28, 2024 09:54
Retry the connection in the `StreamSocketTransporter.php` file when establishing a stream socket client. This change improves the reliability of the connection by attempting to connect multiple times before throwing a connection exception.

Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
…#39)

* Fix: Ensure unpacked data is an array before using array_key_exists()

* Add validation for unpacked data in Client

Throws a RecvFailedException if the unpacked data is not an array, improving error handling for invalid responses.

* Fix: Ensure unpacked data is treated as an array in Client

* chore: Add allow-plugins configuration for composer-normalize

* Fix: Ensure unpacked data is treated as an array and improve error handling in Client

* Fix: Improve error message for invalid data in ServerException

---------

Co-authored-by: 范冠登 <guandeng@addcn.com>
Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Jun 22, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 148b0116-2e02-47b0-b6e9-44d1148c2e0f

📥 Commits

Reviewing files that changed from the base of the PR and between 694aed3 and 074c186.

📒 Files selected for processing (62)
  • .gitattributes
  • .github/workflows/release.yaml
  • .github/workflows/tests.yaml
  • .gitignore
  • .php-cs-fixer.php
  • .vscode/cspell.json
  • README.md
  • bootstrap.php
  • classmap/GuzzleHttp/Client.php
  • composer.json
  • phpunit.xml.dist
  • src/Client.php
  • src/ClientFactory.php
  • src/Consul/Agent.php
  • src/Consul/Catalog.php
  • src/Consul/Client.php
  • src/Consul/Health.php
  • src/Consul/Response.php
  • src/Contract/DataFormatterInterface.php
  • src/Contract/LoadBalancerInterface.php
  • src/Contract/PackerInterface.php
  • src/Contract/PathGeneratorInterface.php
  • src/Contract/RegistryInterface.php
  • src/Contract/TransporterInterface.php
  • src/DataFormatter/DataFormatter.php
  • src/DataFormatter/MultiplexDataFormatter.php
  • src/Exception/ClientException.php
  • src/Exception/ConnectionException.php
  • src/Exception/ExceptionThrower.php
  • src/Exception/JetException.php
  • src/Exception/NoNodesAvailableException.php
  • src/Exception/RecvFailedException.php
  • src/Exception/ServerException.php
  • src/Facade.php
  • src/Functions.php
  • src/LoadBalancer/AbstractLoadBalancer.php
  • src/LoadBalancer/Node.php
  • src/LoadBalancer/Random.php
  • src/LoadBalancer/RoundRobin.php
  • src/Metadata.php
  • src/MetadataManager.php
  • src/Packer/JsonEofPacker.php
  • src/Packer/JsonLengthPacker.php
  • src/Packer/JsonMultiplexPacker.php
  • src/PathGenerator/DotPathGenerator.php
  • src/PathGenerator/FullPathGenerator.php
  • src/PathGenerator/PathGenerator.php
  • src/Registry/ConsulRegistry.php
  • src/RegistryManager.php
  • src/ServiceManager.php
  • src/Support/Arr.php
  • src/Support/Str.php
  • src/Support/UserAgent.php
  • src/Transporter/AbstractTransporter.php
  • src/Transporter/GuzzleHttpTransporter.php
  • src/Transporter/MultiplexRpcTransporter.php
  • src/Transporter/StreamSocketTransporter.php
  • src/helpers.php
  • tests/ClientTest.php
  • tests/RegistryTest.php
  • tests/TestCase.php
  • tests/register.php

📝 Walkthrough

Walkthrough

This PR refactors friendsofhyperf/jet from jet branding to friendsofhyperf/jet. It introduces multiplexed RPC transport (MultiplexRpcTransporter, JsonMultiplexPacker, MultiplexDataFormatter), a fluent immutable Metadata API with with* clone methods, a MetadataManager named registry, namespaced Arr/Str/UserAgent/Functions support classes replacing global helpers in src/helpers.php, a new NoNodesAvailableException, retry-based socket connection, and ClientFactory refactoring.

Changes

Multiplexed RPC, Fluent Metadata, and Support Refactoring

Layer / File(s) Summary
Namespaced support utilities
src/Support/Arr.php, src/Support/Str.php, src/Support/UserAgent.php, src/Functions.php, composer.json
Introduces Arr (dot-notation helpers), Str (casing/replacement), UserAgent (static builder), and Functions.php (retry, throw_if, tap, with, value); removes src/helpers.php global functions; autoload updated from helpers.php to Functions.php.
Exception hierarchy: NoNodesAvailableException and cleanup
src/Exception/NoNodesAvailableException.php, src/Exception/..., src/LoadBalancer/Random.php, src/LoadBalancer/RoundRobin.php, src/LoadBalancer/...
Adds NoNodesAvailableException; simplifies JetException, ClientException, ConnectionException, RecvFailedException to single-line bodies; ServerException constructor updated to ?\Throwable; ExceptionThrower gains strict_types; load balancers throw NoNodesAvailableException instead of RuntimeException.
Metadata fluent with* API and MetadataManager
src/Metadata.php, src/MetadataManager.php
Metadata gains optional-name constructor and nine with* clone-and-return methods; existing set* mutators are marked @deprecated. MetadataManager introduced as a static named registry returning cloned Metadata instances.
Multiplexed RPC: packer, formatter, and transporter
src/Packer/JsonMultiplexPacker.php, src/DataFormatter/MultiplexDataFormatter.php, src/Transporter/MultiplexRpcTransporter.php
JsonMultiplexPacker encodes with 8-byte length+timestamp binary prefix; MultiplexDataFormatter formats id-keyed request/response/errorResponse payloads; MultiplexRpcTransporter reads 4-byte length frames, skips PING/PONG frames, returns frame data via readBytes using stream_select.
StreamSocketTransporter: retry-based connect and microsecond timeout
src/Transporter/StreamSocketTransporter.php
connect() wraps stream_socket_client in retry(5, ...) with isConnected/client assignment inside the callback; receive() computes microsecond-scaled $timeoutMs and doubles it between retries.
GuzzleHttpTransporter and UserAgent wiring
src/Transporter/GuzzleHttpTransporter.php
Constructor switches from array_merge_recursive to array_replace, calls parent::__construct, and replaces ClientFactory::getUserAgent() with UserAgent::get().
ClientFactory refactoring and Client response handling
src/ClientFactory.php, src/Client.php
ClientFactory::create() gains early-return for ServiceManager hit, MetadataManager fast-path for named string transporter, and replaces set* mutation with with* calls; user-agent methods removed. Client::__call() casts unpack result to array and throws ServerException with code/message payload on missing result.
ConsulRegistry, PathGenerators, RegistryManager: Arr/Str migration
src/Registry/ConsulRegistry.php, src/PathGenerator/..., src/Consul/Response.php, src/RegistryManager.php, src/ServiceManager.php, src/Facade.php
ConsulRegistry replaces array_get with Arr::get; path generators replace str_snake/str_studly/str_replace_array with Str helpers; Response::json() accepts ?string $key and uses Arr::get; managers switch to fully-qualified exception references.
Tests, CI, and tooling
tests/TestCase.php, tests/ClientTest.php, phpunit.xml.dist, .github/workflows/*, .gitattributes, README.md, composer.json, .php-cs-fixer.php, .gitignore, .vscode/cspell.json, tests/register.php, tests/RegistryTest.php, bootstrap.php, src/Contract/..., src/Packer/JsonEofPacker.php, src/Packer/JsonLengthPacker.php, src/Transporter/AbstractTransporter.php, src/DataFormatter/DataFormatter.php, src/LoadBalancer/AbstractLoadBalancer.php, src/LoadBalancer/Node.php, src/Consul/...
TestCase gains createMultiplexRpcTransporter() and JSONRPC_LENGTH_CHECK_* env config; two new client tests cover multiplex transport and MetadataManager; CI adds max-parallel/fail-fast, separates Consul/demo startup; release workflow added; README QuickStart updated to fluent API; all file headers rebranded to friendsofhyperf/jet.

Sequence Diagram(s)

sequenceDiagram
  participant CallerCode
  participant ClientFactory
  participant MetadataManager
  participant ServiceManager
  participant Metadata
  participant Client
  participant MultiplexRpcTransporter
  participant JsonMultiplexPacker

  CallerCode->>ClientFactory: create(name, transporter)
  ClientFactory->>ServiceManager: get(name)
  alt found in ServiceManager
    ServiceManager-->>ClientFactory: Metadata
    ClientFactory-->>CallerCode: new Client(metadata)
  else string transporter → MetadataManager lookup
    ClientFactory->>MetadataManager: get(transporter)
    MetadataManager-->>ClientFactory: cloned Metadata (renamed)
    ClientFactory-->>CallerCode: new Client(metadata)
  else build from options
    ClientFactory->>Metadata: new Metadata(name) + withTransporter/withRegistry/...
    Metadata-->>ClientFactory: configured Metadata
    ClientFactory-->>CallerCode: new Client(metadata)
  end

  CallerCode->>Client: __call("add", [1, 2])
  Client->>JsonMultiplexPacker: pack(request)
  JsonMultiplexPacker-->>Client: binary frame (8-byte header + JSON)
  Client->>MultiplexRpcTransporter: send(binary frame)
  MultiplexRpcTransporter-->>Client: recv → header+body bytes
  Client->>JsonMultiplexPacker: unpack(bytes)
  JsonMultiplexPacker-->>Client: assoc array {id, result}
  Client-->>CallerCode: result value
Loading

Estimated code review effort

🎯 5 (Critical) | ⏱️ ~120 minutes

Possibly related PRs

  • friendsofhyperf/jet#40: Introduces MultiplexRpcTransporter with the same PING/PONG-skipping length-framed receive loop implemented in this PR.

Poem

🐇 Hop hop, the helpers found a home,
In namespaced classes they now roam!
Multiplex frames fly swift and true,
With with* clones the metadata grew.
Old globals gone, new classes shine —
This bunny thinks the code is fine! ✨

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch 10969/tests
⚔️ Resolve merge conflicts
  • Resolve merge conflict in branch 10969/tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 PHPStan (2.2.2)

PHPStan was skipped because the user-provided config is missing the required paths: directive.


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.

@xuanyanwow xuanyanwow closed this Jun 22, 2026
@huangdijia
huangdijia deleted the 10969/tests branch June 22, 2026 10:44
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.

3 participants