Last active
March 7, 2021 21:25
-
-
Save pior/4262747b95879e4df57eb61472cfac6f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app | |
import ( | |
"context" | |
"flag" | |
"reflect" | |
"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 Database struct{} | |
func NewDatabase(cfg *Config) *Database { | |
return &Database{} | |
} | |
func (d *Database) Run(ctx context.Context) error { | |
log("database has started") | |
<-ctx.Done() | |
log("database has stopped") | |
return nil | |
} | |
type Server struct{} | |
func NewServer(cfg *Config, cli *CLIOption, db *Database) *Server { | |
return &Server{} | |
} | |
func NewServerBase() *Server { | |
return &Server{} | |
} | |
func (s *Server) Run(ctx context.Context) error { | |
log("server is doing something") | |
<-ctx.Done() | |
log("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(NewDatabase) | |
a.AddRunnable(NewServer) | |
err := a.Run(ctx) | |
require.NoError(t, err) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=== RUN Test_App | |
building services: [{*app.Database} {*app.Server}] | |
building: {*app.Database} | |
calling provider: github.com/pior/app.NewDatabase | |
calling provider: github.com/pior/app.BuildConfig | |
calling provider: github.com/pior/app.ParseCLI | |
building: {*app.Server} | |
calling provider: github.com/pior/app.NewServer | |
found value in cache: {*app.Config} | |
found value in cache: {*app.CLIOption} | |
found value in cache: {*app.Database} | |
starting: {*app.Database} | |
found value in cache: {*app.Database} | |
starting: {*app.Server} | |
found value in cache: {*app.Server} | |
started runnable: *app.Server | |
server is doing something | |
started runnable: *app.Database | |
database has started | |
context cancelled, starting shutdown | |
server has stopped | |
component &{} returned: [<error Value>] | |
database has stopped | |
component &{} returned: [<error Value>] | |
--- PASS: Test_App (3.00s) | |
PASS | |
ok github.com/pior/app 3.227s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment