agonesmock

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

README

Agones Mock SDK

This package provides an abstraction layer for the Agones SDK using a Go interface. This allows developers to write game server logic that can run locally without a Kubernetes cluster (using a mock) and seamlessly switch to the real SDK when deployed.

Installation

go get github.com/kennycoder/agones-go-mock

Usage

Import the package in your game server code:

import "github.com/kennycoder/agones-go-mock"
Example

See examples/simple/main.go for a complete working example.

Local Development (Mock Mode)

To run the game server locally using the mock SDK, set the AGONES_ENV environment variable to local. This will print SDK operations to the console.

AGONES_ENV=local go run examples/simple/main.go

Output example:

2023/10/27 10:00:00 Initializing MOCK SDK...
[2023-10-27T10:00:00Z] Agones Mock: Ready called
[2023-10-27T10:00:00Z] Agones Mock [Alpha]: SetPlayerCapacity called with args: [100]
Production (Real Mode)

When deploying to an Agones cluster, run without the environment variable (or set it to anything else). The application will attempt to connect to the Agones sidecar.

go run examples/simple/main.go

Note: This will fail locally if the Agones sidecar is not present, which is expected behavior for the real SDK.

Project Structure

  • interface.go: Defines the AgonesSDK interface, covering Lifecycle, Health, Metadata, and Alpha features.
  • mock.go: A mock implementation of the SDK that logs operations to standard output instead of communicating with the Agones sidecar.
  • real.go: A wrapper around the official Agones Go SDK that satisfies the AgonesSDK interface.
  • examples/: Contains example usage of the package.

Implementation Details

Your game logic should depend on the agonesmock.AgonesSDK interface rather than the concrete SDK struct.

type GameServerLogic struct {
    agones agonesmock.AgonesSDK
}

This dependency injection pattern makes unit testing easier and decouples your game logic from the infrastructure.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AgonesSDK

type AgonesSDK interface {
	// Lifecycle
	Ready() error
	Allocate() error
	Shutdown() error
	Reserve(duration time.Duration) error

	// Health
	Health() error

	// Metadata
	SetLabel(key, value string) error
	SetAnnotation(key, value string) error

	// Data
	// We use interface{} here to avoid strict dependency on the official sdk.GameServer struct
	// in your tests, but you can cast it in your application code.
	GameServer() (interface{}, error)
	WatchGameServer(f func(gs interface{})) error

	// Features
	Alpha() AlphaFeature
}

AgonesSDK defines the contract that both the Real and Mock SDKs must satisfy.

type AlphaFeature

type AlphaFeature interface {
	PlayerConnect(id string) (bool, error)
	PlayerDisconnect(id string) (bool, error)
	SetPlayerCapacity(capacity int64) error
	GetPlayerCapacity() (int64, error)
	GetPlayerCount() (int64, error)
	IsPlayerConnected(id string) (bool, error)
	GetConnectedPlayers() ([]string, error)
}

AlphaFeature defines the contract for Player Tracking (Alpha).

type MockAlpha

type MockAlpha struct{}

MockAlpha handles the Alpha feature set (Player Tracking, etc.)

func (*MockAlpha) GetConnectedPlayers

func (a *MockAlpha) GetConnectedPlayers() ([]string, error)

func (*MockAlpha) GetPlayerCapacity

func (a *MockAlpha) GetPlayerCapacity() (int64, error)

func (*MockAlpha) GetPlayerCount

func (a *MockAlpha) GetPlayerCount() (int64, error)

func (*MockAlpha) IsPlayerConnected

func (a *MockAlpha) IsPlayerConnected(id string) (bool, error)

func (*MockAlpha) PlayerConnect

func (a *MockAlpha) PlayerConnect(id string) (bool, error)

func (*MockAlpha) PlayerDisconnect

func (a *MockAlpha) PlayerDisconnect(id string) (bool, error)

func (*MockAlpha) SetPlayerCapacity

func (a *MockAlpha) SetPlayerCapacity(capacity int64) error

type MockSDK

type MockSDK struct {
	// contains filtered or unexported fields
}

MockSDK mimics the Agones SDK behavior by printing logs.

func NewMockSDK

func NewMockSDK() (*MockSDK, error)

NewMockSDK initializes the mock and the nested Alpha mock.

func (*MockSDK) Allocate

func (m *MockSDK) Allocate() error

func (*MockSDK) Alpha

func (m *MockSDK) Alpha() AlphaFeature

func (*MockSDK) GameServer

func (m *MockSDK) GameServer() (interface{}, error)

GameServer usually returns *sdk.GameServer. We return nil here to keep this mock dependency-free.

func (*MockSDK) Health

func (m *MockSDK) Health() error

func (*MockSDK) Ready

func (m *MockSDK) Ready() error

func (*MockSDK) Reserve

func (m *MockSDK) Reserve(duration time.Duration) error

func (*MockSDK) SetAnnotation

func (m *MockSDK) SetAnnotation(key, value string) error

func (*MockSDK) SetLabel

func (m *MockSDK) SetLabel(key, value string) error

func (*MockSDK) Shutdown

func (m *MockSDK) Shutdown() error

func (*MockSDK) WatchGameServer

func (m *MockSDK) WatchGameServer(f func(gs interface{})) error

WatchGameServer usually takes a callback function.

type RealSDK

type RealSDK struct {
	// contains filtered or unexported fields
}

RealSDK wraps the official SDK to satisfy the AgonesSDK interface.

func NewRealSDK

func NewRealSDK() (*RealSDK, error)

func (*RealSDK) Allocate

func (r *RealSDK) Allocate() error

func (*RealSDK) Alpha

func (r *RealSDK) Alpha() AlphaFeature

Alpha Wrapper We need this because r.client.Alpha() returns *sdk.Alpha (struct), but our interface expects AlphaFeature (interface).

func (*RealSDK) GameServer

func (r *RealSDK) GameServer() (interface{}, error)

Data methods - Returning the raw struct as interface{}

func (*RealSDK) Health

func (r *RealSDK) Health() error

func (*RealSDK) Ready

func (r *RealSDK) Ready() error

Passthrough methods

func (*RealSDK) Reserve

func (r *RealSDK) Reserve(d time.Duration) error

func (*RealSDK) SetAnnotation

func (r *RealSDK) SetAnnotation(k, v string) error

func (*RealSDK) SetLabel

func (r *RealSDK) SetLabel(k, v string) error

func (*RealSDK) Shutdown

func (r *RealSDK) Shutdown() error

func (*RealSDK) WatchGameServer

func (r *RealSDK) WatchGameServer(f func(interface{})) error

Directories

Path Synopsis
examples
simple command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL