testing

package
v0.9.13 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package testing provides test helpers for the cache package.

Usage:

func TestMyFeature(t *testing.T) {
    // Create a fake Redis store for testing
    store, cleanup := testing.FakeRedis(t, "myapp")
    defer cleanup()

    // Use the store in your tests
    store.Put("key", "value", time.Hour)
    // ...
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FakeManager

func FakeManager(t testing.TB, prefix string) (*cache.Manager, func())

FakeManager creates a cache manager configured with a fake Redis store. Useful for testing code that uses the cache.Manager interface.

Example:

func TestWithManager(t *testing.T) {
    manager, cleanup := testing.FakeManager(t, "app")
    defer cleanup()

    manager.Put("key", "value", time.Hour)
    // ...
}

func FakeManagerMemory

func FakeManagerMemory(t testing.TB, prefix string) (*cache.Manager, func())

FakeManagerMemory creates a cache manager configured with an in-memory store. Faster than FakeManager since it doesn't need miniredis.

Example:

func TestWithMemoryManager(t *testing.T) {
    manager, cleanup := testing.FakeManagerMemory(t, "app")
    defer cleanup()

    manager.Put("key", "value", time.Hour)
    // ...
}

func FakeMemory

func FakeMemory(t testing.TB, prefix string) (*drivers.MemoryStore, func())

FakeMemory creates an in-memory cache store for testing. It returns the store and a cleanup function that should be called with defer.

Example:

func TestCache(t *testing.T) {
    store, cleanup := testing.FakeMemory(t, "app")
    defer cleanup()

    store.Put("key", "value", time.Hour)
    // ...
}

func FakeRedis

func FakeRedis(t testing.TB, prefix string) (*drivers.RedisStore, func())

FakeRedis creates a fake Redis store backed by miniredis for testing. It returns the store and a cleanup function that should be called with defer.

Example:

func TestCache(t *testing.T) {
    store, cleanup := testing.FakeRedis(t, "app")
    defer cleanup()

    store.Put("user:1", map[string]string{"name": "John"}, time.Hour)
    value, found := store.Get("user:1")
    // ...
}

func FakeRedisWithServer

func FakeRedisWithServer(t testing.TB, prefix string) (*drivers.RedisStore, *miniredis.Miniredis, func())

FakeRedisWithServer creates a fake Redis store and also returns the miniredis server for advanced testing scenarios (e.g., simulating time, network errors).

Example:

func TestCacheExpiration(t *testing.T) {
    store, server, cleanup := testing.FakeRedisWithServer(t, "app")
    defer cleanup()

    store.Put("key", "value", 100*time.Millisecond)
    server.FastForward(200 * time.Millisecond)

    _, found := store.Get("key")
    if found {
        t.Error("expected key to be expired")
    }
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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