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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//! Integration tests for wrapper lifecycle
//!
//! Tests for shutdown, flush, and multiple batch operations
use arrow_zerobus_sdk_wrapper::{WrapperConfiguration, ZerobusWrapper, ZerobusError};
use arrow::array::{Int64Array, StringArray};
use arrow::datatypes::{DataType, Field, Schema};
use arrow::record_batch::RecordBatch;
use std::sync::Arc;
use tokio::time::{sleep, Duration};
/// Create a test RecordBatch
fn create_test_batch() -> RecordBatch {
let schema = Schema::new(vec![
Field::new("id", DataType::Int64, false),
Field::new("name", DataType::Utf8, false),
]);
let id_array = Int64Array::from(vec![1, 2, 3]);
let name_array = StringArray::from(vec!["Alice", "Bob", "Charlie"]);
RecordBatch::try_new(
Arc::new(schema),
vec![Arc::new(id_array), Arc::new(name_array)],
)
.unwrap()
}
#[tokio::test]
#[ignore] // Requires actual Zerobus SDK - run manually with real credentials
async fn test_wrapper_shutdown_with_active_operations() {
// Test shutdown while batch is being sent
// Verify graceful shutdown
let config = WrapperConfiguration::new(
"https://test.cloud.databricks.com".to_string(),
"test_table".to_string(),
)
.with_credentials(
std::env::var("ZEROBUS_CLIENT_ID").unwrap_or_else(|_| "test_id".to_string()),
std::env::var("ZEROBUS_CLIENT_SECRET").unwrap_or_else(|_| "test_secret".to_string()),
)
.with_unity_catalog(
std::env::var("UNITY_CATALOG_URL").unwrap_or_else(|_| "https://test".to_string()),
);
let wrapper_result = ZerobusWrapper::new(config).await;
match wrapper_result {
Ok(wrapper) => {
// Start sending a batch
let batch = create_test_batch();
let send_handle = tokio::spawn(async move {
wrapper.send_batch(batch).await
});
// Immediately try to shutdown
// Note: This is a simplified test - in practice, shutdown should wait for active operations
// The actual behavior depends on implementation
// Wait a bit for the send to start
sleep(Duration::from_millis(100)).await;
// Shutdown should complete (may wait for active operations or cancel them)
// This test verifies shutdown doesn't panic
}
Err(_) => {
// Expected without real credentials
}
}
}
#[tokio::test]
async fn test_wrapper_flush() {
// Test flush operations
// Verify debug files are flushed
// Verify observability is flushed
let temp_dir = tempfile::tempdir().unwrap();
let debug_dir = temp_dir.path().to_path_buf();
let config = WrapperConfiguration::new(
"https://test.cloud.databricks.com".to_string(),
"test_table".to_string(),
)
.with_debug_output(debug_dir.clone())
.with_debug_flush_interval_secs(1);
let wrapper_result = ZerobusWrapper::new(config).await;
match wrapper_result {
Ok(wrapper) => {
// Flush should succeed even with no data
let result = wrapper.flush().await;
// May succeed or fail depending on implementation
// But should not panic
match result {
Ok(_) => {
// Success - flush completed
}
Err(e) => {
// Expected if no data to flush or without real SDK
assert!(
matches!(
e,
ZerobusError::ConfigurationError(_)
| ZerobusError::ConnectionError(_)
),
"Expected ConfigurationError or ConnectionError, got: {:?}",
e
);
}
}
}
Err(_) => {
// Expected without real credentials
}
}
}
#[tokio::test]
async fn test_wrapper_multiple_batches() {
// Test sending multiple batches sequentially
// Verify state is maintained correctly
let config = WrapperConfiguration::new(
"https://test.cloud.databricks.com".to_string(),
"test_table".to_string(),
)
.with_credentials(
std::env::var("ZEROBUS_CLIENT_ID").unwrap_or_else(|_| "test_id".to_string()),
std::env::var("ZEROBUS_CLIENT_SECRET").unwrap_or_else(|_| "test_secret".to_string()),
)
.with_unity_catalog(
std::env::var("UNITY_CATALOG_URL").unwrap_or_else(|_| "https://test".to_string()),
);
let wrapper_result = ZerobusWrapper::new(config).await;
match wrapper_result {
Ok(wrapper) => {
// Send multiple batches
for i in 0..5 {
let batch = create_test_batch();
let result = wrapper.send_batch(batch).await;
// May succeed or fail, but should not panic
match result {
Ok(transmission_result) => {
// Success
assert!(transmission_result.attempts >= 1);
assert!(transmission_result.batch_size_bytes > 0);
}
Err(e) => {
// Failure is acceptable without real credentials
// But verify it's a known error type
assert!(
matches!(
e,
ZerobusError::ConfigurationError(_)
| ZerobusError::AuthenticationError(_)
| ZerobusError::ConnectionError(_)
),
"Batch {} failed with unexpected error: {:?}",
i,
e
);
}
}
// Small delay between batches
sleep(Duration::from_millis(10)).await;
}
}
Err(_) => {
// Expected without real credentials
}
}
}
#[tokio::test]
async fn test_wrapper_shutdown_after_use() {
// Test shutdown after using the wrapper
let config = WrapperConfiguration::new(
"https://test.cloud.databricks.com".to_string(),
"test_table".to_string(),
);
let wrapper_result = ZerobusWrapper::new(config).await;
match wrapper_result {
Ok(wrapper) => {
// Shutdown should succeed
let result = wrapper.shutdown().await;
// May succeed or fail, but should not panic
match result {
Ok(_) => {
// Success - shutdown completed
}
Err(e) => {
// Expected if there were active operations or without real SDK
assert!(
matches!(
e,
ZerobusError::ConfigurationError(_)
| ZerobusError::ConnectionError(_)
),
"Expected ConfigurationError or ConnectionError, got: {:?}",
e
);
}
}
}
Err(_) => {
// Expected without real credentials
}
}
}
#[tokio::test]
async fn test_wrapper_flush_with_debug_enabled() {
// Test flush when debug is enabled
let temp_dir = tempfile::tempdir().unwrap();
let debug_dir = temp_dir.path().to_path_buf();
let config = WrapperConfiguration::new(
"https://test.cloud.databricks.com".to_string(),
"test_table".to_string(),
)
.with_debug_output(debug_dir.clone())
.with_debug_enabled(true);
let wrapper_result = ZerobusWrapper::new(config).await;
match wrapper_result {
Ok(wrapper) => {
// Send a batch to generate debug output
let batch = create_test_batch();
let _ = wrapper.send_batch(batch).await; // Ignore result
// Flush should write debug files
let result = wrapper.flush().await;
// May succeed or fail, but should not panic
match result {
Ok(_) => {
// Success - debug files flushed
}
Err(_) => {
// Expected if no data or without real SDK
}
}
}
Err(_) => {
// Expected without real credentials
}
}
}
#[tokio::test]
async fn test_wrapper_lifecycle_complete() {
// Test complete lifecycle: create -> use -> flush -> shutdown
let config = WrapperConfiguration::new(
"https://test.cloud.databricks.com".to_string(),
"test_table".to_string(),
)
.with_credentials(
std::env::var("ZEROBUS_CLIENT_ID").unwrap_or_else(|_| "test_id".to_string()),
std::env::var("ZEROBUS_CLIENT_SECRET").unwrap_or_else(|_| "test_secret".to_string()),
)
.with_unity_catalog(
std::env::var("UNITY_CATALOG_URL").unwrap_or_else(|_| "https://test".to_string()),
);
let wrapper_result = ZerobusWrapper::new(config).await;
match wrapper_result {
Ok(wrapper) => {
// Step 1: Use wrapper
let batch = create_test_batch();
let _ = wrapper.send_batch(batch).await; // Ignore result
// Step 2: Flush
let _ = wrapper.flush().await; // Ignore result
// Step 3: Shutdown
let _ = wrapper.shutdown().await; // Ignore result
// If we get here without panicking, lifecycle is complete
}
Err(_) => {
// Expected without real credentials
}
}
}
#[tokio::test]
async fn test_wrapper_initializes_without_credentials_when_writer_disabled() {
// Test that wrapper can be initialized without credentials when writer is disabled
use tempfile::TempDir;
let temp_dir = TempDir::new().unwrap();
let debug_output_dir = temp_dir.path().to_path_buf();
let config = WrapperConfiguration::new(
"https://test.cloud.databricks.com".to_string(),
"test_table".to_string(),
)
.with_debug_output(debug_output_dir)
.with_zerobus_writer_disabled(true);
// No credentials provided
let wrapper_result = ZerobusWrapper::new(config).await;
// Should succeed without credentials when writer is disabled
assert!(wrapper_result.is_ok(), "Wrapper should initialize without credentials when writer disabled");
let wrapper = wrapper_result.unwrap();
let batch = create_test_batch();
// Send batch should succeed (writes debug files, skips SDK calls)
let result = wrapper.send_batch(batch).await;
assert!(result.is_ok(), "send_batch should succeed when writer disabled");
let transmission_result = result.unwrap();
assert!(transmission_result.success, "Transmission should indicate success");
}