interface.go 1.75 KB
Newer Older
1 2
// Package mockrouting provides a virtual routing server. To use it,
// create a virtual routing server and use the Client() method to get a
Raúl Kripalani's avatar
Raúl Kripalani committed
3
// routing client (Routing). The server quacks like a DHT but is
4
// really a local in-memory hash table.
Brian Tiger Chow's avatar
Brian Tiger Chow committed
5 6 7
package mockrouting

import (
8 9
	"context"

tavit ohanian's avatar
tavit ohanian committed
10 11
	ds "gitlab.dms3.io/dms3/public/go-datastore"
	delay "gitlab.dms3.io/dms3/public/go-dms3-delay"
Raúl Kripalani's avatar
Raúl Kripalani committed
12

tavit ohanian's avatar
tavit ohanian committed
13 14 15
	"gitlab.dms3.io/p2p/go-p2p-core/peer"
	"gitlab.dms3.io/p2p/go-p2p-core/routing"
	tnet "gitlab.dms3.io/p2p/go-p2p-testing/net"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
16 17
)

Steven Allen's avatar
Steven Allen committed
18 19 20 21 22 23
// MockValidator is a record validator that always returns success.
type MockValidator struct{}

func (MockValidator) Validate(_ string, _ []byte) error        { return nil }
func (MockValidator) Select(_ string, _ [][]byte) (int, error) { return 0, nil }

Brian Tiger Chow's avatar
Brian Tiger Chow committed
24 25
// Server provides mockrouting Clients
type Server interface {
Raúl Kripalani's avatar
Raúl Kripalani committed
26 27
	Client(p tnet.Identity) Client
	ClientWithDatastore(context.Context, tnet.Identity, ds.Datastore) Client
Brian Tiger Chow's avatar
Brian Tiger Chow committed
28 29
}

Raúl Kripalani's avatar
Raúl Kripalani committed
30
// Client implements Routing
Brian Tiger Chow's avatar
Brian Tiger Chow committed
31
type Client interface {
Raúl Kripalani's avatar
Raúl Kripalani committed
32
	routing.Routing
Brian Tiger Chow's avatar
Brian Tiger Chow committed
33 34 35 36
}

// NewServer returns a mockrouting Server
func NewServer() Server {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
37 38 39 40
	return NewServerWithDelay(DelayConfig{
		ValueVisibility: delay.Fixed(0),
		Query:           delay.Fixed(0),
	})
Brian Tiger Chow's avatar
Brian Tiger Chow committed
41 42 43
}

// NewServerWithDelay returns a mockrouting Server with a delay!
Brian Tiger Chow's avatar
Brian Tiger Chow committed
44
func NewServerWithDelay(conf DelayConfig) Server {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
45
	return &s{
46
		providers: make(map[string]map[peer.ID]providerRecord),
Brian Tiger Chow's avatar
Brian Tiger Chow committed
47
		delayConf: conf,
Brian Tiger Chow's avatar
Brian Tiger Chow committed
48 49
	}
}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
50

51 52
// DelayConfig can be used to configured the fake delays of a mock server.
// Use with NewServerWithDelay().
Brian Tiger Chow's avatar
Brian Tiger Chow committed
53 54 55 56 57 58 59 60
type DelayConfig struct {
	// ValueVisibility is the time it takes for a value to be visible in the network
	// FIXME there _must_ be a better term for this
	ValueVisibility delay.D

	// Query is the time it takes to receive a response from a routing query
	Query delay.D
}