|
1 | 1 | package quickfix |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path" |
4 | 8 | "strings" |
5 | 9 | "testing" |
6 | 10 | ) |
@@ -45,3 +49,82 @@ SessionQualifier=BS |
45 | 49 | t.Error("Should have returned factory") |
46 | 50 | } |
47 | 51 | } |
| 52 | + |
| 53 | +type fileLogHelper struct { |
| 54 | + LogPath string |
| 55 | + Prefix string |
| 56 | + Log Log |
| 57 | +} |
| 58 | + |
| 59 | +func newFileLogHelper(t *testing.T) *fileLogHelper { |
| 60 | + prefix := "myprefix" |
| 61 | + logPath := path.Join(os.TempDir(), fmt.Sprintf("TestLogStore-%d", os.Getpid())) |
| 62 | + |
| 63 | + log, err := newFileLog(prefix, logPath) |
| 64 | + if err != nil { |
| 65 | + t.Error("Unexpected error", err) |
| 66 | + } |
| 67 | + |
| 68 | + return &fileLogHelper{ |
| 69 | + LogPath: logPath, |
| 70 | + Prefix: prefix, |
| 71 | + Log: log, |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestNewFileLog(t *testing.T) { |
| 76 | + helper := newFileLogHelper(t) |
| 77 | + |
| 78 | + tests := []struct { |
| 79 | + expectedPath string |
| 80 | + }{ |
| 81 | + {path.Join(helper.LogPath, fmt.Sprintf("%v.messages.current.log", helper.Prefix))}, |
| 82 | + {path.Join(helper.LogPath, fmt.Sprintf("%v.event.current.log", helper.Prefix))}, |
| 83 | + } |
| 84 | + |
| 85 | + for _, test := range tests { |
| 86 | + if _, err := os.Stat(test.expectedPath); os.IsNotExist(err) { |
| 87 | + t.Errorf("%v does not exist", test.expectedPath) |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestFileLog_Append(t *testing.T) { |
| 93 | + helper := newFileLogHelper(t) |
| 94 | + |
| 95 | + messageLogFile, err := os.Open(path.Join(helper.LogPath, fmt.Sprintf("%v.messages.current.log", helper.Prefix))) |
| 96 | + if err != nil { |
| 97 | + t.Error("Unexpected error", err) |
| 98 | + } |
| 99 | + defer messageLogFile.Close() |
| 100 | + |
| 101 | + eventLogFile, err := os.Open(path.Join(helper.LogPath, fmt.Sprintf("%v.event.current.log", helper.Prefix))) |
| 102 | + if err != nil { |
| 103 | + t.Error("Unexpected error", err) |
| 104 | + } |
| 105 | + defer eventLogFile.Close() |
| 106 | + |
| 107 | + messageScanner := bufio.NewScanner(messageLogFile) |
| 108 | + eventScanner := bufio.NewScanner(eventLogFile) |
| 109 | + |
| 110 | + helper.Log.OnIncoming("incoming") |
| 111 | + if !messageScanner.Scan() { |
| 112 | + t.Error("Unexpected EOF") |
| 113 | + } |
| 114 | + |
| 115 | + helper.Log.OnEvent("Event") |
| 116 | + if !eventScanner.Scan() { |
| 117 | + t.Error("Unexpected EOF") |
| 118 | + } |
| 119 | + |
| 120 | + newHelper := newFileLogHelper(t) |
| 121 | + newHelper.Log.OnIncoming("incoming") |
| 122 | + if !messageScanner.Scan() { |
| 123 | + t.Error("Unexpected EOF") |
| 124 | + } |
| 125 | + |
| 126 | + newHelper.Log.OnEvent("Event") |
| 127 | + if !eventScanner.Scan() { |
| 128 | + t.Error("Unexpected EOF") |
| 129 | + } |
| 130 | +} |
0 commit comments