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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//! SDK-backed message fixtures for downstream tests, without a Bloomberg session.
use std::ffi::CString;
use std::ptr::{self, NonNull};
use blpapi_sys as sdk;
use crate::{CorrelationId, Event, EventType, Name};
/// Keeps a test event's deserialized schema alive until after the event is released.
pub struct TestEvent {
event: Event,
_service: Option<TestService>,
}
impl TestEvent {
/// Construct one subscription message using an explicit SDK service schema.
///
/// TestUtil materializes every field declared in the schema, including omitted
/// nullable JSON fields. To represent absence, omit the field from the schema.
pub fn subscription(
schema: &str,
message_type: &str,
format: impl FnOnce(&mut TestMessageFormatter),
) -> Self {
Self::message(
Some(schema),
EventType::SubscriptionData,
message_type,
&[],
format,
)
}
/// Construct a message with an explicit schema, event kind and correlation IDs.
pub fn with_schema(
schema: &str,
event_type: EventType,
message_type: &str,
correlation_ids: &[i64],
format: impl FnOnce(&mut TestMessageFormatter),
) -> Self {
Self::message(
Some(schema),
event_type,
message_type,
correlation_ids,
format,
)
}
/// Construct an SDK-defined administrative or status message.
pub fn admin(
event_type: EventType,
message_type: &str,
correlation_ids: &[i64],
format: impl FnOnce(&mut TestMessageFormatter),
) -> Self {
Self::message(None, event_type, message_type, correlation_ids, format)
}
fn message(
schema: Option<&str>,
event_type: EventType,
message_type: &str,
correlation_ids: &[i64],
format: impl FnOnce(&mut TestMessageFormatter),
) -> Self {
let name = Name::get_or_intern(message_type);
let mut definition = ptr::null_mut();
let service = if let Some(schema) = schema {
let mut service = ptr::null_mut();
// SAFETY: schema is readable for its stated length; the output is initialized on success.
assert_eq!(
unsafe {
sdk::blpapi_TestUtil_deserializeService(
schema.as_ptr().cast(),
schema.len(),
&mut service,
)
},
0,
"deserialize test service"
);
let service = TestService(NonNull::new(service).expect("test service"));
// SAFETY: service and name remain alive while the borrowed definition is used.
assert_eq!(
unsafe {
sdk::blpapi_Service_getEventDefinition(
service.0.as_ptr(),
&mut definition,
ptr::null(),
name.as_ptr(),
)
},
0,
"test message definition"
);
Some(service)
} else {
// SAFETY: the SDK owns its built-in definitions; name remains live for the call.
assert_eq!(
unsafe {
sdk::blpapi_TestUtil_getAdminMessageDefinition(&mut definition, name.as_ptr())
},
0,
"test administrative definition"
);
None
};
let mut raw_event = ptr::null_mut();
// SAFETY: the successful call transfers one owned event reference.
assert_eq!(
unsafe { sdk::blpapi_TestUtil_createEvent(&mut raw_event, event_type.to_raw()) },
0,
"create test event"
);
let raw_event = NonNull::new(raw_event).expect("test event");
// SAFETY: adopt that owned reference exactly once using the core ownership boundary.
let event = unsafe { Event::from_raw(raw_event.as_ptr()) };
Self::append_raw(raw_event, definition, correlation_ids, format);
Self {
event,
_service: service,
}
}
/// Append another message to the same SDK event. A mutable borrow prevents
/// existing message views from being used while the event is extended.
pub fn append_message(
&mut self,
message_type: &str,
correlation_ids: &[i64],
format: impl FnOnce(&mut TestMessageFormatter),
) {
let name = Name::get_or_intern(message_type);
let mut definition = ptr::null_mut();
// SAFETY: this fixture keeps its service alive; built-in definitions are SDK-owned.
let result = unsafe {
if let Some(service) = &self._service {
sdk::blpapi_Service_getEventDefinition(
service.0.as_ptr(),
&mut definition,
ptr::null(),
name.as_ptr(),
)
} else {
sdk::blpapi_TestUtil_getAdminMessageDefinition(&mut definition, name.as_ptr())
}
};
assert_eq!(result, 0, "test message definition");
Self::append_raw(
NonNull::new(self.event.as_ptr()).expect("live test event"),
definition,
correlation_ids,
format,
);
}
fn append_raw(
event: NonNull<sdk::blpapi_Event_t>,
definition: *mut sdk::blpapi_SchemaElementDefinition_t,
correlation_ids: &[i64],
format: impl FnOnce(&mut TestMessageFormatter),
) {
let mut properties = ptr::null_mut();
// SAFETY: properties is an initialized output pointer.
assert_eq!(
unsafe { sdk::blpapi_MessageProperties_create(&mut properties) },
0
);
let properties = TestMessageProperties(NonNull::new(properties).expect("test properties"));
if !correlation_ids.is_empty() {
let correlation_ids: Vec<_> = correlation_ids
.iter()
.map(|&id| CorrelationId::new_int(id).to_ffi())
.collect();
// SAFETY: the SDK copies these initialized IDs into the live properties.
assert_eq!(
unsafe {
sdk::blpapi_MessageProperties_setCorrelationIds(
properties.0.as_ptr(),
correlation_ids.as_ptr(),
correlation_ids.len(),
)
},
0,
"set test correlation IDs",
);
}
let mut formatter = ptr::null_mut();
// SAFETY: the event owner, schema owner and properties outlive this formatter.
assert_eq!(
unsafe {
sdk::blpapi_TestUtil_appendMessage(
&mut formatter,
event.as_ptr(),
definition,
properties.0.as_ptr(),
)
},
0,
"append test message",
);
let mut formatter = TestMessageFormatter(NonNull::new(formatter).expect("test formatter"));
format(&mut formatter);
}
pub fn event(&self) -> &Event {
&self.event
}
}
struct TestService(NonNull<sdk::blpapi_Service_t>);
impl Drop for TestService {
fn drop(&mut self) {
// SAFETY: TestUtil gave this owner one service reference; the event drops first.
unsafe { sdk::blpapi_Service_release(self.0.as_ptr()) };
}
}
struct TestMessageProperties(NonNull<sdk::blpapi_MessageProperties_t>);
impl Drop for TestMessageProperties {
fn drop(&mut self) {
// SAFETY: this owns the successfully created properties pointer.
unsafe { sdk::blpapi_MessageProperties_destroy(self.0.as_ptr()) };
}
}
pub struct TestMessageFormatter(NonNull<sdk::blpapi_MessageFormatter_t>);
impl TestMessageFormatter {
pub fn json(&mut self, value: &str) {
let value = CString::new(value).expect("fixture JSON has no NUL");
// SAFETY: formatter is live and value is a valid C string for the call.
assert_eq!(
unsafe {
sdk::blpapi_MessageFormatter_FormatMessageJson(self.0.as_ptr(), value.as_ptr())
},
0,
"format test JSON"
);
}
pub fn char(&mut self, field: &str, value: Option<u8>) {
let field = Name::get_or_intern(field);
// SAFETY: formatter and field name remain live during each typed SDK call.
let result = unsafe {
match value {
Some(value) => sdk::blpapi_MessageFormatter_setValueChar(
self.0.as_ptr(),
field.as_ptr(),
value as std::ffi::c_char,
),
None => sdk::blpapi_MessageFormatter_setValueNull(self.0.as_ptr(), field.as_ptr()),
}
};
assert_eq!(result, 0, "format test Char");
}
pub fn datetime(&mut self, field: &str, value: &sdk::blpapi_HighPrecisionDatetime_t) {
let field = Name::get_or_intern(field);
// SAFETY: formatter, field and the initialized datetime remain live for the call.
assert_eq!(
unsafe {
sdk::blpapi_MessageFormatter_setValueHighPrecisionDatetime(
self.0.as_ptr(),
field.as_ptr(),
value,
)
},
0,
"format test datetime"
);
}
}
impl Drop for TestMessageFormatter {
fn drop(&mut self) {
// SAFETY: this owns the formatter created by appendMessage.
unsafe { sdk::blpapi_MessageFormatter_destroy(self.0.as_ptr()) };
}
}