feat: game lobby service
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
// Package gmclientstub provides an in-process ports.GMClient
|
||||
// implementation used by service-level and worker-level tests that do
|
||||
// not need to spin up an httptest server. The stub records every
|
||||
// register call and every liveness probe, and supports independent
|
||||
// error injection for each method so and paths can
|
||||
// be exercised separately.
|
||||
//
|
||||
// Production code never wires this stub.
|
||||
package gmclientstub
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
|
||||
"galaxy/lobby/internal/ports"
|
||||
)
|
||||
|
||||
// Client is a concurrency-safe in-memory ports.GMClient.
|
||||
type Client struct {
|
||||
mu sync.Mutex
|
||||
err error
|
||||
pingErr error
|
||||
requests []ports.RegisterGameRequest
|
||||
pingCalls int
|
||||
}
|
||||
|
||||
// NewClient constructs an empty Client.
|
||||
func NewClient() *Client {
|
||||
return &Client{}
|
||||
}
|
||||
|
||||
// SetError makes the next RegisterGame calls return err. Passing nil
|
||||
// clears the override.
|
||||
func (client *Client) SetError(err error) {
|
||||
client.mu.Lock()
|
||||
defer client.mu.Unlock()
|
||||
client.err = err
|
||||
}
|
||||
|
||||
// SetPingError makes the next Ping calls return err. Passing nil
|
||||
// clears the override. RegisterGame is unaffected.
|
||||
func (client *Client) SetPingError(err error) {
|
||||
client.mu.Lock()
|
||||
defer client.mu.Unlock()
|
||||
client.pingErr = err
|
||||
}
|
||||
|
||||
// Requests returns the ordered slice of register requests received.
|
||||
func (client *Client) Requests() []ports.RegisterGameRequest {
|
||||
client.mu.Lock()
|
||||
defer client.mu.Unlock()
|
||||
return append([]ports.RegisterGameRequest(nil), client.requests...)
|
||||
}
|
||||
|
||||
// PingCalls returns the number of Ping invocations observed so far.
|
||||
func (client *Client) PingCalls() int {
|
||||
client.mu.Lock()
|
||||
defer client.mu.Unlock()
|
||||
return client.pingCalls
|
||||
}
|
||||
|
||||
// RegisterGame records the request and returns the configured error.
|
||||
func (client *Client) RegisterGame(ctx context.Context, request ports.RegisterGameRequest) error {
|
||||
if ctx == nil {
|
||||
return errors.New("register game: nil context")
|
||||
}
|
||||
client.mu.Lock()
|
||||
defer client.mu.Unlock()
|
||||
if client.err != nil {
|
||||
return client.err
|
||||
}
|
||||
client.requests = append(client.requests, request)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Ping increments the call counter and returns the configured error.
|
||||
func (client *Client) Ping(ctx context.Context) error {
|
||||
if ctx == nil {
|
||||
return errors.New("ping: nil context")
|
||||
}
|
||||
client.mu.Lock()
|
||||
defer client.mu.Unlock()
|
||||
client.pingCalls++
|
||||
return client.pingErr
|
||||
}
|
||||
|
||||
// Compile-time interface assertion.
|
||||
var _ ports.GMClient = (*Client)(nil)
|
||||
Reference in New Issue
Block a user