-
Notifications
You must be signed in to change notification settings - Fork 211
/
poet_test_harness.go
84 lines (69 loc) · 1.89 KB
/
poet_test_harness.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package activation
import (
"context"
"net/url"
"time"
"github.com/spacemeshos/poet/config"
"github.com/spacemeshos/poet/server"
"github.com/spacemeshos/poet/service"
)
// HTTPPoetTestHarness utilizes a local self-contained poet server instance
// targeted by an HTTP client. It is intended to be used in tests only.
type HTTPPoetTestHarness struct {
*HTTPPoetClient
Service *server.Server
}
type HTTPPoetOpt func(*config.Config)
func WithGenesis(genesis time.Time) HTTPPoetOpt {
return func(cfg *config.Config) {
cfg.Service.Genesis = service.Genesis(genesis)
}
}
func WithEpochDuration(epoch time.Duration) HTTPPoetOpt {
return func(cfg *config.Config) {
cfg.Service.EpochDuration = epoch
}
}
func WithPhaseShift(phase time.Duration) HTTPPoetOpt {
return func(cfg *config.Config) {
cfg.Service.PhaseShift = phase
}
}
func WithCycleGap(gap time.Duration) HTTPPoetOpt {
return func(cfg *config.Config) {
cfg.Service.CycleGap = gap
}
}
// NewHTTPPoetTestHarness returns a new instance of HTTPPoetHarness.
func NewHTTPPoetTestHarness(ctx context.Context, poetdir string, opts ...HTTPPoetOpt) (*HTTPPoetTestHarness, error) {
cfg := config.DefaultConfig()
cfg.PoetDir = poetdir
cfg.RawRESTListener = "localhost:0"
cfg.RawRPCListener = "localhost:0"
for _, opt := range opts {
opt(cfg)
}
cfg, err := config.SetupConfig(cfg)
if err != nil {
return nil, err
}
poet, err := server.New(ctx, *cfg)
if err != nil {
return nil, err
}
// NewHTTPPoetClient takes an URL as connection string. Server speaks HTTP.
url := &url.URL{
Scheme: "http",
Host: poet.GrpcRestProxyAddr().String(),
}
client, err := NewHTTPPoetClient(url.String(), DefaultPoetConfig())
if err != nil {
return nil, err
}
// TODO: query for the REST address to allow dynamic port allocation.
// It needs changes in poet.
return &HTTPPoetTestHarness{
HTTPPoetClient: client,
Service: poet,
}, nil
}