forked from NemoMi/zeroeye
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdelta_validation_test.go
More file actions
131 lines (111 loc) · 3.51 KB
/
Copy pathdelta_validation_test.go
File metadata and controls
131 lines (111 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Fix for Issue #4: [$45 BOUNTY] [Go] Add WebSocket order book delta validation tests
// market/ws/test_helpers.go
package ws
import (
"encoding/json"
"errors"
"sync"
)
// MockWebSocketConn provides a deterministic WebSocket connection for testing
type MockWebSocketConn struct {
mu sync.Mutex
messages [][]byte
messageIndex int
closed bool
writeBuffer [][]byte
}
// NewMockWebSocketConn creates a new mock connection with predefined messages
func NewMockWebSocketConn(messages [][]byte) *MockWebSocketConn {
return &MockWebSocketConn{
messages: messages,
messageIndex: 0,
writeBuffer: make([][]byte, 0),
}
}
// ReadMessage returns the next predefined message
func (m *MockWebSocketConn) ReadMessage() (messageType int, p []byte, err error) {
m.mu.Lock()
defer m.mu.Unlock()
if m.closed {
return 0, nil, errors.New("connection closed")
}
if m.messageIndex >= len(m.messages) {
return 0, nil, errors.New("no more messages")
}
msg := m.messages[m.messageIndex]
m.messageIndex++
return 1, msg, nil // 1 = TextMessage
}
// WriteMessage stores the message in the write buffer
func (m *MockWebSocketConn) WriteMessage(messageType int, data []byte) error {
m.mu.Lock()
defer m.mu.Unlock()
if m.closed {
return errors.New("connection closed")
}
m.writeBuffer = append(m.writeBuffer, data)
return nil
}
// Close marks the connection as closed
func (m *MockWebSocketConn) Close() error {
m.mu.Lock()
defer m.mu.Unlock()
m.closed = true
return nil
}
// GetWrittenMessages returns all messages written to the connection
func (m *MockWebSocketConn) GetWrittenMessages() [][]byte {
m.mu.Lock()
defer m.mu.Unlock()
return m.writeBuffer
}
// OrderBookDelta represents a delta update to the order book
type OrderBookDelta struct {
Type string `json:"type"`
Symbol string `json:"symbol"`
Sequence int64 `json:"sequence"`
Bids [][]interface{} `json:"bids,omitempty"`
Asks [][]interface{} `json:"asks,omitempty"`
Timestamp int64 `json:"timestamp"`
Checksum string `json:"checksum,omitempty"`
}
// OrderBookSnapshot represents a full order book snapshot
type OrderBookSnapshot struct {
Type string `json:"type"`
Symbol string `json:"symbol"`
Sequence int64 `json:"sequence"`
Bids [][]interface{} `json:"bids"`
Asks [][]interface{} `json:"asks"`
Timestamp int64 `json:"timestamp"`
Checksum string `json:"checksum,omitempty"`
}
// CreateDeltaMessage creates a JSON-encoded delta message
func CreateDeltaMessage(delta OrderBookDelta) []byte {
data, _ := json.Marshal(delta)
return data
}
// CreateSnapshotMessage creates a JSON-encoded snapshot message
func CreateSnapshotMessage(snapshot OrderBookSnapshot) []byte {
data, _ := json.Marshal(snapshot)
return data
}
// DeltaValidationError represents a validation error for delta processing
type DeltaValidationError struct {
Code string `json:"code"`
Message string `json:"message"`
Field string `json:"field,omitempty"`
}
func (e *DeltaValidationError) Error() string {
return e.Message
}
// ValidationErrorCodes for delta validation
const (
ErrCodeMalformedPrice = "MALFORMED_PRICE"
ErrCodeMalformedQuantity = "MALFORMED_QUANTITY"
ErrCodeMalformedSide = "MALFORMED_SIDE"
ErrCodeMalformedSymbol = "MALFORMED_SYMBOL"
ErrCodeStaleSequence = "STALE_SEQUENCE"
ErrCodeOutOfOrder = "OUT_OF_ORDER_SEQUENCE"
ErrCodeChecksumMismatch = "CHECKSUM_MISMATCH"
ErrCodeInvalidPayload = "INVALID_PAYLOAD"
)