Skip to content

Commit b781a85

Browse files
committed
Adds composite log
1 parent f01e28e commit b781a85

File tree

2 files changed

+190
-0
lines changed

2 files changed

+190
-0
lines changed

log/composite/composite_log.go

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright (c) quickfixengine.org All rights reserved.
2+
//
3+
// This file may be distributed under the terms of the quickfixengine.org
4+
// license as defined by quickfixengine.org and appearing in the file
5+
// LICENSE included in the packaging of this file.
6+
//
7+
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
8+
// THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A
9+
// PARTICULAR PURPOSE.
10+
//
11+
// See http://www.quickfixengine.org/LICENSE for licensing information.
12+
//
13+
// Contact [email protected] if any conditions of this licensing
14+
// are not clear to you.
15+
16+
package composite
17+
18+
import (
19+
"github.com/quickfixgo/quickfix"
20+
)
21+
22+
type compositeLog struct {
23+
logs []quickfix.Log
24+
}
25+
26+
func (l compositeLog) OnIncoming(s []byte) {
27+
for _, log := range l.logs {
28+
log.OnIncoming(s)
29+
}
30+
}
31+
32+
func (l compositeLog) OnOutgoing(s []byte) {
33+
for _, log := range l.logs {
34+
log.OnOutgoing(s)
35+
}
36+
}
37+
38+
func (l compositeLog) OnEvent(s string) {
39+
for _, log := range l.logs {
40+
log.OnEvent(s)
41+
}
42+
}
43+
44+
func (l compositeLog) OnEventf(format string, a ...interface{}) {
45+
for _, log := range l.logs {
46+
log.OnEventf(format, a)
47+
}
48+
}
49+
50+
type compositeLogFactory struct {
51+
logFactories []quickfix.LogFactory
52+
}
53+
54+
func (clf compositeLogFactory) Create() (quickfix.Log, error) {
55+
logs := []quickfix.Log{}
56+
for _, lf := range clf.logFactories {
57+
log, err := lf.Create()
58+
if err != nil {
59+
return nil, err
60+
}
61+
logs = append(logs, log)
62+
}
63+
return compositeLog{logs}, nil
64+
}
65+
66+
func (clf compositeLogFactory) CreateSessionLog(sessionID quickfix.SessionID) (quickfix.Log, error) {
67+
logs := []quickfix.Log{}
68+
for _, lf := range clf.logFactories {
69+
log, err := lf.CreateSessionLog(sessionID)
70+
if err != nil {
71+
return nil, err
72+
}
73+
logs = append(logs, log)
74+
}
75+
return compositeLog{logs}, nil
76+
}
77+
78+
// NewLogFactory creates an instance of LogFactory that writes messages and events to stdout.
79+
func NewLogFactory(logfactories []quickfix.LogFactory) quickfix.LogFactory {
80+
return compositeLogFactory{logfactories}
81+
}

log/composite/composite_log_test.go

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Copyright (c) quickfixengine.org All rights reserved.
2+
//
3+
// This file may be distributed under the terms of the quickfixengine.org
4+
// license as defined by quickfixengine.org and appearing in the file
5+
// LICENSE included in the packaging of this file.
6+
//
7+
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
8+
// THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A
9+
// PARTICULAR PURPOSE.
10+
//
11+
// See http://www.quickfixengine.org/LICENSE for licensing information.
12+
//
13+
// Contact [email protected] if any conditions of this licensing
14+
// are not clear to you.
15+
16+
package composite
17+
18+
import (
19+
"fmt"
20+
"log"
21+
"os"
22+
"path"
23+
"strings"
24+
"testing"
25+
"time"
26+
27+
_ "github.com/mattn/go-sqlite3"
28+
"github.com/quickfixgo/quickfix"
29+
"github.com/quickfixgo/quickfix/log/file"
30+
"github.com/quickfixgo/quickfix/log/mongo"
31+
"github.com/quickfixgo/quickfix/log/screen"
32+
"github.com/quickfixgo/quickfix/log/sql"
33+
"github.com/stretchr/testify/require"
34+
"github.com/stretchr/testify/suite"
35+
)
36+
37+
// CompositeLogTestSuite runs tests for the MongoLog impl of Log.
38+
type CompositeLogTestSuite struct {
39+
suite.Suite
40+
sqlLogRootPath string
41+
settings *quickfix.Settings
42+
sessionID quickfix.SessionID
43+
}
44+
45+
func (suite *CompositeLogTestSuite) SetupTest() {
46+
mongoDbCxn := os.Getenv("MONGODB_TEST_CXN")
47+
if len(mongoDbCxn) <= 0 {
48+
log.Println("MONGODB_TEST_CXN environment arg is not provided, skipping...")
49+
suite.T().SkipNow()
50+
}
51+
mongoDatabase := "automated_testing_database"
52+
mongoReplicaSet := "replicaset"
53+
54+
// create settings
55+
sessionID := quickfix.SessionID{BeginString: "FIX.4.4", SenderCompID: "SENDER", TargetCompID: "TARGET"}
56+
logPath := path.Join(os.TempDir(), fmt.Sprintf("TestLogStore-%d", os.Getpid()))
57+
suite.sqlLogRootPath = path.Join(os.TempDir(), fmt.Sprintf("SQLLogTestSuite-%d", os.Getpid()))
58+
err := os.MkdirAll(suite.sqlLogRootPath, os.ModePerm)
59+
require.Nil(suite.T(), err)
60+
sqlDriver := "sqlite3"
61+
sqlDsn := path.Join(suite.sqlLogRootPath, fmt.Sprintf("%d.db", time.Now().UnixNano()))
62+
63+
settings, err := quickfix.ParseSettings(strings.NewReader(fmt.Sprintf(`
64+
[DEFAULT]
65+
MongoLogConnection=%s
66+
MongoLogDatabase=%s
67+
MongoLogReplicaSet=%s
68+
FileLogPath=%s
69+
SQLLogDriver=%s
70+
SQLLogDataSourceName=%s
71+
SQLLogConnMaxLifetime=14400s
72+
73+
[SESSION]
74+
BeginString=%s
75+
SenderCompID=%s
76+
TargetCompID=%s`, mongoDbCxn, mongoDatabase, mongoReplicaSet, logPath, sqlDriver, sqlDsn, sessionID.BeginString, sessionID.SenderCompID, sessionID.TargetCompID)))
77+
require.Nil(suite.T(), err)
78+
79+
suite.sessionID = sessionID
80+
suite.settings = settings
81+
}
82+
83+
func (suite *CompositeLogTestSuite) TestCreateLogNoSession() {
84+
85+
mngoLogFactory := mongo.NewLogFactory(suite.settings)
86+
sqlLogFactory := sql.NewLogFactory(suite.settings)
87+
// create log
88+
_, err := NewLogFactory([]quickfix.LogFactory{mngoLogFactory, sqlLogFactory}).Create()
89+
require.Nil(suite.T(), err)
90+
}
91+
92+
func (suite *CompositeLogTestSuite) TestCreateLogSession() {
93+
94+
screenLogFactory := screen.NewLogFactory()
95+
fileLogFactory, err := file.NewLogFactory(suite.settings)
96+
require.Nil(suite.T(), err)
97+
98+
// create log
99+
_, err = NewLogFactory([]quickfix.LogFactory{screenLogFactory, fileLogFactory}).CreateSessionLog(suite.sessionID)
100+
require.Nil(suite.T(), err)
101+
}
102+
103+
func (suite *CompositeLogTestSuite) TearDownTest() {
104+
os.RemoveAll(suite.sqlLogRootPath)
105+
}
106+
107+
func TestCompositeLogTestSuite(t *testing.T) {
108+
suite.Run(t, new(CompositeLogTestSuite))
109+
}

0 commit comments

Comments
 (0)