-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(allsrv): add allsrvc CLI companion
Again here we start with a little refactoring. This time we're creating a new `allsrvtesting` pkg to hold the reusable code bits. Now we can implement our CLI and then create the svc implemenation utilizing the cli in our tests. With a handful of lines of code we're able to create a high degree of certainty in the implementation of our CLI. The implementation and testing both, are not limited by the SVC behavior. You can extend the CLI with additional behavior beyond that of the SVC. Additional tests can be added to accomodate any additional behavior. Here we are at the end of the session where we've matured an intensely immature service implementation. We've covered a lot of ground. We can sum it up quickly with: 1. Tests provide certainty we've not broken the existing implementation 2. Versioning an API helps us transition into the replacement * note: we determined in that the v1 was not serving our users best interest, so we moved onto a structured JSON-API spec 3. Observability is SUUUPER important. Just like with testing, we want to keep a close eye on our metrics. We want to make sure our changes are improving the bottom line. Without this information, we're flying blind. 4. Isolating the SVC/usecases from the transport & db layers gives us freedom to reuse that business logic across any number of transport & db layers, improve our observability stack along the way, and allow us to create a reusable test suite that is usable across any implementation of the SVC. With that test suite, creating and verifying a new SVC implementation.
- Loading branch information
Showing
11 changed files
with
391 additions
and
140 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package allsrvtesting | ||
|
||
import ( | ||
"log/slog" | ||
"testing" | ||
|
||
"github.com/hashicorp/go-metrics" | ||
|
||
"github.com/jsteenb2/mess/allsrv" | ||
) | ||
|
||
func NewInmemSVC(t *testing.T, opts SVCTestOpts) allsrv.SVC { | ||
db := new(allsrv.InmemDB) | ||
opts.PrepDB(t, db) | ||
|
||
var svc allsrv.SVC = allsrv.NewService(db, opts.SVCOpts...) | ||
svc = allsrv.SVCLogging(newTestLogger(t))(svc) | ||
svc = allsrv.ObserveSVC(metrics.Default())(svc) | ||
|
||
return svc | ||
} | ||
|
||
func newTestLogger(t *testing.T) *slog.Logger { | ||
return slog.New(slog.NewJSONHandler(&testr{t: t}, nil)) | ||
} | ||
|
||
type testr struct { | ||
t *testing.T | ||
} | ||
|
||
func (t *testr) Write(p []byte) (n int, err error) { | ||
t.t.Log(string(p)) | ||
return len(p), nil | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package allsrvtesting | ||
|
||
import ( | ||
"strconv" | ||
"time" | ||
) | ||
|
||
func IDGen(start, incr int) func() string { | ||
return func() string { | ||
id := strconv.Itoa(start) | ||
start += incr | ||
return id | ||
} | ||
} | ||
|
||
func NowFn(start time.Time, incr time.Duration) func() time.Time { | ||
return func() time.Time { | ||
t := start | ||
start = start.Add(incr) | ||
return t | ||
} | ||
} | ||
|
||
func Ptr[T any](v T) *T { | ||
return &v | ||
} |
Oops, something went wrong.