-
Notifications
You must be signed in to change notification settings - Fork 1
/
app_test.go
82 lines (64 loc) · 1.41 KB
/
app_test.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
package mikado
import (
"context"
"flag"
"log"
"testing"
"time"
"github.com/stretchr/testify/require"
)
type CLIOption struct {
configFile string
}
func ParseCLI() (*CLIOption, error) {
configFile := flag.String("config", "~/.appconfig", "path to the config file")
flag.Parse()
return &CLIOption{*configFile}, nil
}
type Config struct {
DatabaseHost string
}
func BuildConfig(cliOption *CLIOption) *Config {
// use cliOption.configFile to load the config
return &Config{
DatabaseHost: "127.0.0.1",
}
}
type Store interface {
List() []string
}
type MemoryStore struct{}
func NewMemoryStore() Store {
return &MemoryStore{}
}
func (d *MemoryStore) List() []string {
return []string{"one", "two"}
}
func (d *MemoryStore) Run(ctx context.Context) error {
log.Print("database has started")
<-ctx.Done()
log.Print("database has stopped")
return nil
}
type Server struct{}
func NewServer(cfg *Config, cli *CLIOption, store Store) *Server {
return &Server{}
}
func (s *Server) Run(ctx context.Context) error {
log.Print("server is doing something")
<-ctx.Done()
log.Print("server has stopped")
return nil
}
func Test_App(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
a := New()
a.AddProvider(ParseCLI)
a.AddProvider(BuildConfig)
a.AddRunnable(NewMemoryStore)
a.AddRunnable(NewServer)
err := a.Run(ctx)
require.NoError(t, err)
}