|
| 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