github.com/m-lab/locate@v0.17.6/heartbeat/heartbeattest/heartbeattest.go (about)

     1  package heartbeattest
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/gomodule/redigo/redis"
     7  	v2 "github.com/m-lab/locate/api/v2"
     8  	"github.com/m-lab/locate/memorystore"
     9  )
    10  
    11  var (
    12  	// FakeMemorystoreClient provides an implementation of MemorystoreClient for testing.
    13  	FakeMemorystoreClient = fakeMemorystoreClient[v2.HeartbeatMessage]{
    14  		m: make(map[string]v2.HeartbeatMessage),
    15  	}
    16  	// FakeErrorMemorystoreClient provides an implementation that returns errors
    17  	// for all its methods.
    18  	FakeErrorMemorystoreClient = fakeErrorMemorystoreClient[v2.HeartbeatMessage]{}
    19  	// FakeError defines an error to be returned by the implementation of FakeErrorMemorystoreClient.
    20  	FakeError = errors.New("error for FakeErrorMemorystoreClient")
    21  )
    22  
    23  type fakeMemorystoreClient[V any] struct {
    24  	m map[string]V
    25  }
    26  
    27  // Put returns nil.
    28  func (c *fakeMemorystoreClient[V]) Put(key string, field string, value redis.Scanner, opts *memorystore.PutOptions) error {
    29  	return nil
    30  }
    31  
    32  // GetAll returns an empty map and a nil error.
    33  func (c *fakeMemorystoreClient[V]) GetAll() (map[string]V, error) {
    34  	return c.m, nil
    35  }
    36  
    37  // FakeAdd mimics adding a new value to Memorystore for testing.
    38  func (c *fakeMemorystoreClient[V]) FakeAdd(key string, value V) {
    39  	c.m[key] = value
    40  }
    41  
    42  type fakeErrorMemorystoreClient[V any] struct{}
    43  
    44  // Put returns a FakeError.
    45  func (c *fakeErrorMemorystoreClient[V]) Put(key string, field string, value redis.Scanner, opts *memorystore.PutOptions) error {
    46  	return FakeError
    47  }
    48  
    49  // GetAll returns an empty map and a FakeError.
    50  func (c *fakeErrorMemorystoreClient[V]) GetAll() (map[string]V, error) {
    51  	return map[string]V{}, FakeError
    52  }
    53  
    54  // FakeStatusTracker provides a fake implementation of HeartbeatStatusTracker.
    55  type FakeStatusTracker struct {
    56  	Err           error
    57  	FakeInstances map[string]v2.HeartbeatMessage
    58  }
    59  
    60  // RegisterInstance returns the FakeStatusTracker's Err field.
    61  func (t *FakeStatusTracker) RegisterInstance(rm v2.Registration) error {
    62  	return t.Err
    63  }
    64  
    65  // UpdateHealth returns the FakeStatusTracker's Err field.
    66  func (t *FakeStatusTracker) UpdateHealth(hostname string, hm v2.Health) error {
    67  	return t.Err
    68  }
    69  
    70  // UpdatePrometheus returns the FakeStatusTracker's Err field.
    71  func (t *FakeStatusTracker) UpdatePrometheus(hostnames, machines map[string]bool) error {
    72  	return t.Err
    73  }
    74  
    75  // Instances returns nil.
    76  func (t *FakeStatusTracker) Instances() map[string]v2.HeartbeatMessage {
    77  	if t.FakeInstances != nil {
    78  		return t.FakeInstances
    79  	}
    80  	return nil
    81  }
    82  
    83  // Ready returns true when Err is nil, false otherwise.
    84  func (t *FakeStatusTracker) Ready() bool {
    85  	return t.Err == nil
    86  }
    87  
    88  // StopImport does nothing.
    89  func (t *FakeStatusTracker) StopImport() {}