Skip to content

Commit

Permalink
Update database integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roblaszczak committed Jul 30, 2020
1 parent ebe2ed7 commit 521fdb5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .test.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FIRESTORE_EMULATOR_HOST=localhost:8787
MYSQL_ADDR=localhost
16 changes: 15 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
include .env
include .test.env
export

.PHONY: openapi
openapi: openapi_http openapi_js
Expand Down Expand Up @@ -44,4 +46,16 @@ lint:

.PHONY: mycli
mycli:
mycli -u ${MYSQL_USER} -p ${MYSQL_PASSWORD} ${MYSQL_DATABASE}
mycli -u ${MYSQL_USER} -p ${MYSQL_PASSWORD} ${MYSQL_DATABASE}

INERNAL_PACKAGES := $(wildcard internal/*)

ifeq (test,$(firstword $(MAKECMDGOALS)))
TEST_ARGS := $(subst $$,$$$$,$(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS)))
$(eval $(TEST_ARGS):;@:)
endif

.PHONY: test $(INERNAL_PACKAGES)
test: $(INERNAL_PACKAGES)
$(INERNAL_PACKAGES):
@(cd $@ && go test -count=1 -race ./... $(subst $$$$,$$,$(TEST_ARGS)))
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ No application is perfect from the beginning. With over a dozen coming articles,
5. [**Business Applications in Go: Things to know about DRY**](https://threedots.tech/post/things-to-know-about-dry/?utm_source=github.com)
6. [**When microservices in Go are not enough: introduction to DDD Lite**](https://threedots.tech/post/ddd-lite-in-go-introduction/?utm_source=github.com)
7. [**Repository pattern: painless way to simplify your Go service logic**](https://threedots.tech/post/repository-pattern-in-go/?utm_source=github.com)
8. *More articles are on the way!*
8. [**4 practical principles of high-quality database integration tests in Go**](https://threedots.tech/post/database-integration-testing/?utm_source=github.com)
9. *More articles are on the way!*

### Directories

Expand Down
1 change: 1 addition & 0 deletions internal/common/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/common
go 1.14

require (
cloud.google.com/go v0.38.0
firebase.google.com/go v3.12.0+incompatible
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/go-chi/chi v4.1.0+incompatible
Expand Down
10 changes: 9 additions & 1 deletion internal/trainer/hour_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,14 @@ func testUpdateHour_parallel(t *testing.T, repository hour.Repository) {
})
require.NoError(t, err)

workersCount := 10
workersCount := 20
workersDone := sync.WaitGroup{}
workersDone.Add(workersCount)

// closing startWorkers will unblock all workers at once,
// thanks to that it will be more likely to have race condition
startWorkers := make(chan struct{})
// if training was successfully scheduled, number of the worker is sent to this channel
trainingsScheduled := make(chan int, workersCount)

// we are trying to do race condition, in practice only one worker should be able to finish transaction
Expand All @@ -163,9 +166,11 @@ func testUpdateHour_parallel(t *testing.T, repository hour.Repository) {
schedulingTraining := false

err := repository.UpdateHour(ctx, hourTime, func(h *hour.Hour) (*hour.Hour, error) {
// training is already scheduled, nothing to do there
if h.HasTrainingScheduled() {
return h, nil
}
// training is not scheduled yet, so let's try to do that
if err := h.ScheduleTraining(); err != nil {
return nil, err
}
Expand All @@ -176,12 +181,15 @@ func testUpdateHour_parallel(t *testing.T, repository hour.Repository) {
})

if schedulingTraining && err == nil {
// training is only scheduled if UpdateHour didn't return an error
trainingsScheduled <- workerNum
}
}()
}

close(startWorkers)

// we are waiting, when all workers did the job
workersDone.Wait()
close(trainingsScheduled)

Expand Down

0 comments on commit 521fdb5

Please sign in to comment.