gripmock

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2025 License: Apache-2.0 Imports: 23 Imported by: 0

README

Gripmock Embedded

Fork of tokopedia/gripmock - Embedded gripmock library for Go that provides in-process gRPC mocking without external dependencies.

This is a fork of the original gripmock project, modified to support embedded/in-process gRPC mocking for better integration testing experience.

Quick Start

1. Initialize embedded gripmock in your TestMain:
func TestMain(m *testing.M) {
    err := gripmock.InitEmbeddedGripmock("../../../protos", []int{4771, 4772, 4773, 4774, 4775})
    if err != nil {
        panic(fmt.Sprintf("Failed to initialize embedded gripmock: %v", err))
    }
    defer gripmock.StopEmbeddedGripmock()

    // Run all tests
    os.Exit(m.Run())
}
2. Use in tests:
func TestExample(t *testing.T) {
    // Clear any existing stubs
    err := gripmock.Clear()
    require.NoError(t, err)

    // Add stubs
    err = gripmock.AddStub("proto_internal.ProtoInternalService", "transfer_tokens", nil, nil)
    require.NoError(t, err)

    // Your logic tests go here...
}

File Structure

  • gripmock.go - Core server implementation
  • embedded.go - Main API and drop-in replacement functions
  • manager.go - Multi-server management
  • mocker.go - gRPC request handling and protobuf conversion

API Reference

Global Functions
  • InitEmbeddedGripmock(protoDir, ports) - Initialize servers
  • StopEmbeddedGripmock() - Stop all servers
  • AddStub(service, method, input, output) - Add mock stub
  • Clear() - Remove all stubs
  • GetActivePorts() - Get running server ports
  • IsRunning() - Check if servers are running
Advanced Usage

For more control, you can use the underlying types:

// Create individual servers
server, err := gripmock.NewServer(9001, []string{"path/to/service.proto"})
if err != nil {
    log.Fatal(err)
}

// Start server
ctx := context.Background()
if err := server.Start(ctx); err != nil {
    log.Fatal(err)
}
defer server.Stop()

// Create mocker for the server
mocker := gripmock.NewEmbeddedMocker(server)
err = mocker.AddStub("MyService", "MyMethod", inputData, outputData)

Requirements

  • Go 1.24+

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddStub

func AddStub(service, method string, input, output interface{}) error

AddStub adds a stub to all gripmock servers

func AddStubToPort added in v1.1.2

func AddStubToPort(port int, service, method string, input, output interface{}) error

AddStubToPort adds a stub to a specific gripmock server by port

func Clear

func Clear() error

Clear removes all stubs from all servers

func GetActivePorts

func GetActivePorts() []int

GetActivePorts returns the ports of all running gripmock servers Useful for debugging or integration with other services

func InitEmbeddedGripmock

func InitEmbeddedGripmock(protoDir string, ports []int) error

InitEmbeddedGripmock initializes embedded gripmock servers Call this once in TestMain or test setup

func IsRunning

func IsRunning() bool

IsRunning returns true if all gripmock servers are running

func StopEmbeddedGripmock

func StopEmbeddedGripmock()

StopEmbeddedGripmock stops all embedded gripmock servers Call this in test teardown

Types

type EmbeddedMocker

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

EmbeddedMocker provides a convenient interface for working with embedded gripmock

func NewEmbeddedMocker

func NewEmbeddedMocker(server *Server) *EmbeddedMocker

NewEmbeddedMocker creates a new embedded mocker with the given server

func (*EmbeddedMocker) AddStub

func (m *EmbeddedMocker) AddStub(service, method string, input, output interface{}) error

AddStub adds a stub for the given service and method with input/output matching

func (*EmbeddedMocker) Clear

func (m *EmbeddedMocker) Clear()

Clear removes all stubs from the server

func (*EmbeddedMocker) GetServer

func (m *EmbeddedMocker) GetServer() *Server

GetServer returns the underlying server instance

type MultiServerManager

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

MultiServerManager manages multiple embedded gripmock servers

func NewMultiServerManager

func NewMultiServerManager() *MultiServerManager

NewMultiServerManager creates a new manager for multiple gripmock servers

func (*MultiServerManager) AddStub

func (m *MultiServerManager) AddStub(service, method string, input, output interface{}) error

AddStub adds a stub to all running servers

func (*MultiServerManager) Clear

func (m *MultiServerManager) Clear()

Clear removes all stubs from all servers

func (*MultiServerManager) GetServer

func (m *MultiServerManager) GetServer(port int) (*EmbeddedMocker, bool)

GetServer returns the embedded mocker for a specific port

func (*MultiServerManager) GetServerPorts

func (m *MultiServerManager) GetServerPorts() []int

GetServerPorts returns all active server ports

func (*MultiServerManager) IsRunning

func (m *MultiServerManager) IsRunning() bool

IsRunning returns true if all servers are running

func (*MultiServerManager) StartServers

func (m *MultiServerManager) StartServers(ctx context.Context, configs []ServerConfig) error

StartServers starts multiple gripmock servers with the given configurations

func (*MultiServerManager) StopAll

func (m *MultiServerManager) StopAll()

StopAll stops all running servers

type Server

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

Server represents a simplified gRPC mock server

func NewServer

func NewServer(port int, protoFiles []string) (*Server, error)

NewServer creates a new simplified gRPC mock server

func (*Server) AddStub

func (s *Server) AddStub(stub *stuber.Stub) error

AddStub adds a stub to the server

func (*Server) ClearStubs

func (s *Server) ClearStubs()

ClearStubs removes all stubs from the server

func (*Server) GetPort

func (s *Server) GetPort() int

GetPort returns the port the server is listening on

func (*Server) IsRunning

func (s *Server) IsRunning() bool

IsRunning returns true if the server is currently running

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

Start starts the gRPC server on the specified port

func (*Server) Stop

func (s *Server) Stop()

Stop stops the gRPC server

func (*Server) WaitForReady

func (s *Server) WaitForReady(timeout time.Duration) error

WaitForReady waits for the server to be ready to accept connections

type ServerConfig

type ServerConfig struct {
	Port       int
	ProtoDir   string
	Identifier string // optional identifier for logging
}

ServerConfig represents configuration for a single gripmock server

type SimpleMocker

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

Directories

Path Synopsis
internal
pbs
proto
Package domain provides primitives to interact with the openapi HTTP API.
Package domain provides primitives to interact with the openapi HTTP API.

Jump to

Keyboard shortcuts

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