Skip to content

Commit e47bb1a

Browse files
authored
Merge pull request #3 from pvelx/develop
refactoring, clear package
2 parents ac745ee + ba1565f commit e47bb1a

37 files changed

Lines changed: 869 additions & 1054 deletions

.travis.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
language: go
2+
go:
3+
- 1.x
4+
5+
services:
6+
- mysql
7+
8+
before_install:
9+
- mysql -e 'CREATE DATABASE task;'
10+
11+
script:
12+
# - GOMAXPROCS=4 go test ./ -v
13+
# - GOMAXPROCS=4 go test ./repository -v
14+
# - go test ./sender_service ./task_manager ./error_service ./prioritized_task_list ./preloader_service ./monitoring_service ./waiting_service -v
15+
- make test
16+
17+
notifications:
18+
email: false

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@ creating_and_deleting:
33

44
sending_and_confirmation:
55
go run cmd/benchmark/*.go -test_name=sending_and_confirmation -task_count=100000
6+
7+
test:
8+
GOMAXPROCS=4 go test ./ -v
9+
GOMAXPROCS=4 go test ./repository -v
10+
go test ./sender_service ./task_manager ./error_service \
11+
./prioritized_task_list ./preloader_service ./monitoring_service ./waiting_service -v

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[![Build Status](https://travis-ci.com/pvelx/triggerhook.svg?branch=master)](https://travis-ci.com/pvelx/triggerhook)

builder.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
package triggerHook
1+
package triggerhook
22

33
import (
4-
"github.com/pvelx/triggerHook/connection"
5-
"github.com/pvelx/triggerHook/contracts"
6-
"github.com/pvelx/triggerHook/error_service"
7-
"github.com/pvelx/triggerHook/monitoring_service"
8-
"github.com/pvelx/triggerHook/preloader_service"
9-
"github.com/pvelx/triggerHook/repository"
10-
"github.com/pvelx/triggerHook/sender_service"
11-
"github.com/pvelx/triggerHook/task_manager"
12-
"github.com/pvelx/triggerHook/util"
13-
"github.com/pvelx/triggerHook/waiting_service"
4+
"github.com/pvelx/triggerhook/connection"
5+
"github.com/pvelx/triggerhook/contracts"
6+
"github.com/pvelx/triggerhook/error_service"
7+
"github.com/pvelx/triggerhook/monitoring_service"
8+
"github.com/pvelx/triggerhook/preloader_service"
9+
"github.com/pvelx/triggerhook/repository"
10+
"github.com/pvelx/triggerhook/sender_service"
11+
"github.com/pvelx/triggerhook/task_manager"
12+
"github.com/pvelx/triggerhook/util"
13+
"github.com/pvelx/triggerhook/waiting_service"
1414
)
1515

1616
type Config struct {
@@ -24,13 +24,13 @@ type Config struct {
2424
PreloaderServiceOptions preloader_service.Options
2525
}
2626

27-
func Build(config Config) contracts.TasksDeferredInterface {
27+
func Build(config Config) contracts.TriggerHookInterface {
2828

2929
errorService := error_service.New(&config.ErrorServiceOptions)
3030
monitoringService := monitoring_service.New(&config.MonitoringServiceOptions)
3131

3232
repositoryService := repository.New(
33-
connection.NewMysqlClient(config.Connection),
33+
connection.New(&config.Connection),
3434
util.NewId(),
3535
errorService,
3636
&config.RepositoryOptions,

cmd/benchmark/creating_and_deleting.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@ package main
22

33
import (
44
"fmt"
5-
"github.com/cheggaaa/pb/v3"
65
"log"
76
"math/rand"
87
"sync"
98
"time"
109

11-
"github.com/pvelx/triggerHook"
12-
"github.com/pvelx/triggerHook/connection"
13-
"github.com/pvelx/triggerHook/contracts"
14-
"github.com/pvelx/triggerHook/domain"
10+
"github.com/cheggaaa/pb/v3"
11+
"github.com/pvelx/triggerhook"
12+
"github.com/pvelx/triggerhook/connection"
13+
"github.com/pvelx/triggerhook/contracts"
14+
"github.com/pvelx/triggerhook/domain"
1515
)
1616

1717
func creatingAndDeleting(taskCount int) [][]string {
1818
var durationDeleting time.Duration
1919

20-
triggerHookService := triggerHook.Build(triggerHook.Config{
20+
triggerHookService := triggerhook.Build(triggerhook.Config{
2121
Connection: connection.Options{
22-
User: "root",
23-
Password: "secret",
24-
Host: "127.0.0.1:3306",
25-
DbName: "test_db",
22+
User: mysqlUser,
23+
Password: mysqlPassword,
24+
Host: mysqlHost,
25+
DbName: mysqlDbName,
2626
},
2727
})
2828

@@ -55,7 +55,7 @@ func creatingAndDeleting(taskCount int) [][]string {
5555
}
5656
}
5757

58-
func deleteTasks(tasks <-chan *domain.Task, triggerHookService contracts.TasksDeferredInterface) {
58+
func deleteTasks(tasks <-chan *domain.Task, triggerHookService contracts.TriggerHookInterface) {
5959
fmt.Println("\nDeleting task")
6060
preparingBar := pb.StartNew(len(tasks))
6161

@@ -78,7 +78,7 @@ func deleteTasks(tasks <-chan *domain.Task, triggerHookService contracts.TasksDe
7878
}
7979

8080
func createTasks(
81-
triggerHookService contracts.TasksDeferredInterface,
81+
triggerHookService contracts.TriggerHookInterface,
8282
numberOfTask int,
8383
dispersion int,
8484
) <-chan *domain.Task {

cmd/benchmark/main.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ import (
77
"os"
88

99
"github.com/olekukonko/tablewriter"
10-
"github.com/pvelx/triggerHook/connection"
10+
"github.com/pvelx/triggerhook/connection"
11+
)
12+
13+
var (
14+
mysqlUser = os.Getenv("DATABASE_USER")
15+
mysqlPassword = os.Getenv("DATABASE_PASSWORD")
16+
mysqlHost = os.Getenv("DATABASE_HOST")
17+
mysqlDbName = os.Getenv("DATABASE_NAME")
1118
)
1219

1320
func clear() {
14-
conn := connection.NewMysqlClient(connection.Options{
15-
User: "root",
16-
Password: "secret",
17-
Host: "127.0.0.1:3306",
18-
DbName: "test_db",
19-
})
21+
conn := connection.New(nil)
2022
if _, err := conn.Exec("DELETE FROM task"); err != nil {
2123
log.Fatal(err)
2224
}

cmd/benchmark/sending_and_confirmation.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,28 @@ package main
22

33
import (
44
"fmt"
5-
"github.com/cheggaaa/pb/v3"
6-
"github.com/pvelx/triggerHook/error_service"
7-
"github.com/pvelx/triggerHook/monitoring_service"
8-
"github.com/pvelx/triggerHook/repository"
9-
"github.com/pvelx/triggerHook/util"
105
"log"
116
"math/rand"
127
"sync"
138
"time"
149

15-
"github.com/pvelx/triggerHook"
16-
"github.com/pvelx/triggerHook/connection"
17-
"github.com/pvelx/triggerHook/contracts"
18-
"github.com/pvelx/triggerHook/domain"
10+
"github.com/cheggaaa/pb/v3"
11+
"github.com/pvelx/triggerhook"
12+
"github.com/pvelx/triggerhook/connection"
13+
"github.com/pvelx/triggerhook/contracts"
14+
"github.com/pvelx/triggerhook/domain"
15+
"github.com/pvelx/triggerhook/error_service"
16+
"github.com/pvelx/triggerhook/monitoring_service"
17+
"github.com/pvelx/triggerhook/repository"
18+
"github.com/pvelx/triggerhook/util"
1919
)
2020

2121
func upInitialState(taskCount int) {
2222

2323
fmt.Println("\nup initial state")
2424
preparingBar := pb.StartNew(taskCount)
2525

26-
conn := connection.NewMysqlClient(connection.Options{
27-
User: "root",
28-
Password: "secret",
29-
Host: "127.0.0.1:3306",
30-
DbName: "test_db",
31-
})
26+
conn := connection.New(nil)
3227
errorService := error_service.New(nil)
3328
repositoryService := repository.New(conn, util.NewId(), errorService, nil)
3429

@@ -73,17 +68,17 @@ func sendingAndConfirmation(taskCount int) [][]string {
7368
fmt.Println("\nsending/confirmation tasks")
7469
preparingBar := pb.StartNew(taskCount)
7570

76-
triggerHookService := triggerHook.Build(triggerHook.Config{
71+
triggerHookService := triggerhook.Build(triggerhook.Config{
7772
Connection: connection.Options{
78-
User: "root",
79-
Password: "secret",
80-
Host: "127.0.0.1:3306",
81-
DbName: "test_db",
73+
User: mysqlUser,
74+
Password: mysqlPassword,
75+
Host: mysqlHost,
76+
DbName: mysqlDbName,
8277
},
8378
MonitoringServiceOptions: monitoring_service.Options{
8479
PeriodMeasure: 100 * time.Millisecond,
8580
Subscriptions: map[contracts.Topic]func(event contracts.MeasurementEvent){
86-
contracts.SpeedOfConfirmation: func(event contracts.MeasurementEvent) {
81+
contracts.ConfirmationRate: func(event contracts.MeasurementEvent) {
8782
confirmed = confirmed + int(event.Measurement)
8883
preparingBar.Add(int(event.Measurement))
8984
if confirmed == taskCount {

connection/mysql.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package connection
33
import (
44
"database/sql"
55
"fmt"
6+
67
_ "github.com/go-sql-driver/mysql"
78
"github.com/imdario/mergo"
89
)
@@ -16,9 +17,17 @@ type Options struct {
1617
MaxOpenConns int
1718
}
1819

19-
func NewMysqlClient(options Options) *sql.DB {
20+
func New(options *Options) *sql.DB {
21+
22+
if options == nil {
23+
options = &Options{}
24+
}
2025

21-
if err := mergo.Merge(&options, Options{
26+
if err := mergo.Merge(options, Options{
27+
Host: "127.0.0.1:3306",
28+
User: "root",
29+
Password: "",
30+
DbName: "task",
2231
MaxOpenConns: 25,
2332
MaxIdleConns: 25,
2433
}); err != nil {

0 commit comments

Comments
 (0)