optionchain_simulator 0.0.3

OptionChain-Simulator is a lightweight REST API service that simulates an evolving option chain with every request. It is designed for developers building or testing trading systems, backtesters, and visual tools that depend on option data streams but want to avoid relying on live data feeds.
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
use crate::infrastructure::config::redis::RedisConfig;
use redis::{Client, Commands, Connection, RedisError, RedisResult};
use std::sync::{Arc, Mutex};
use tracing::{debug, error, info, instrument};

/// A client for interacting with a Redis database
pub struct RedisClient {
    /// A connection pool (connection wrapped in Mutex for thread safety)
    connection_pool: Arc<Mutex<Connection>>,
    /// Redis configuration
    config: RedisConfig,
}

impl RedisClient {
    /// Creates a new Redis client with the provided configuration
    #[instrument(skip(config), level = "debug")]
    pub fn new(config: RedisConfig) -> Result<Self, RedisError> {
        // Build Redis connection URL
        let url = config.url();
        info!("Connecting to Redis at {}", url);

        // Create Redis client
        let client = Client::open(url)?;

        // Create a single connection for now
        // In a production system, you might want to use a more robust connection pool
        let connection = client.get_connection()?;
        let connection_pool = Arc::new(Mutex::new(connection));

        info!("Successfully connected to Redis");

        Ok(Self {
            connection_pool,
            config,
        })
    }

    /// Gets a connection from the pool
    fn get_connection(&self) -> Result<std::sync::MutexGuard<'_, Connection>, RedisError> {
        self.connection_pool.lock().map_err(|e| {
            error!("Failed to acquire Redis connection lock: {}", e);
            RedisError::from(std::io::Error::other(format!(
                "Failed to acquire Redis connection lock: {}",
                e
            )))
        })
    }

    /// Gets a value from Redis by key
    #[instrument(skip(self), level = "debug")]
    pub fn get<T: redis::FromRedisValue>(&self, key: &str) -> RedisResult<Option<T>> {
        let mut connection = self.get_connection()?;
        let exists: bool = connection.exists(key)?;

        if !exists {
            debug!("Key '{}' not found in Redis", key);
            return Ok(None);
        }

        debug!("Retrieving key '{}' from Redis", key);
        let value: T = connection.get(key)?;
        Ok(Some(value))
    }

    /// Sets a value in Redis with an optional expiration time in seconds
    #[instrument(skip(self, value), level = "debug")]
    pub fn set<T: redis::ToRedisArgs>(
        &self,
        key: &str,
        value: T,
        expiry_secs: Option<u64>,
    ) -> RedisResult<()> {
        let mut connection = self.get_connection()?;

        match expiry_secs {
            Some(secs) => {
                debug!(
                    "Setting key '{}' in Redis with expiry of {} seconds",
                    key, secs
                );
                connection.set_ex(key, value, secs)
            }
            None => {
                debug!("Setting key '{}' in Redis without expiry", key);
                connection.set(key, value)
            }
        }
    }

    /// Deletes a key from Redis
    #[instrument(skip(self), level = "debug")]
    pub fn delete(&self, key: &str) -> RedisResult<bool> {
        let mut connection = self.get_connection()?;
        debug!("Deleting key '{}' from Redis", key);
        let deleted: i32 = connection.del(key)?;
        Ok(deleted > 0)
    }

    /// Gets all keys matching a pattern
    #[instrument(skip(self), level = "debug")]
    pub fn keys(&self, pattern: &str) -> RedisResult<Vec<String>> {
        let mut connection = self.get_connection()?;
        debug!("Getting keys matching pattern '{}' from Redis", pattern);
        connection.keys(pattern)
    }

    /// Checks if a key exists in Redis
    #[instrument(skip(self), level = "debug")]
    pub fn exists(&self, key: &str) -> RedisResult<bool> {
        let mut connection = self.get_connection()?;
        debug!("Checking if key '{}' exists in Redis", key);
        connection.exists(key)
    }

    /// Returns the Redis configuration
    pub fn get_config(&self) -> &RedisConfig {
        &self.config
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use redis::{RedisError, RedisResult};
    use std::sync::{Arc, Mutex};

    // Mock implementation for Redis connection
    // We need to create a custom struct that implements necessary methods
    struct MockConnection {
        exists_results: std::collections::HashMap<String, bool>,
        get_results: std::collections::HashMap<String, String>,
        del_results: std::collections::HashMap<String, i32>,
        keys_results: std::collections::HashMap<String, Vec<String>>,
    }

    impl MockConnection {
        fn new() -> Self {
            Self {
                exists_results: std::collections::HashMap::new(),
                get_results: std::collections::HashMap::new(),
                del_results: std::collections::HashMap::new(),
                keys_results: std::collections::HashMap::new(),
            }
        }

        // Configurators to set expected results
        fn with_exists(mut self, key: &str, result: bool) -> Self {
            self.exists_results.insert(key.to_string(), result);
            self
        }

        fn with_get(mut self, key: &str, result: &str) -> Self {
            self.get_results.insert(key.to_string(), result.to_string());
            self
        }

        fn with_del(mut self, key: &str, result: i32) -> Self {
            self.del_results.insert(key.to_string(), result);
            self
        }

        fn with_keys(mut self, pattern: &str, result: Vec<String>) -> Self {
            self.keys_results.insert(pattern.to_string(), result);
            self
        }

        // Mock implementation of connection methods
        fn exists(&mut self, key: &str) -> RedisResult<bool> {
            match self.exists_results.get(key) {
                Some(result) => Ok(*result),
                None => Err(RedisError::from((
                    redis::ErrorKind::TypeError,
                    "Key not configured in mock",
                ))),
            }
        }

        fn get(&mut self, key: &str) -> RedisResult<String> {
            match self.get_results.get(key) {
                Some(value) => Ok(value.clone()),
                None => Err(RedisError::from((
                    redis::ErrorKind::TypeError,
                    "Key not configured in mock",
                ))),
            }
        }

        fn set(&mut self, _key: &str, _value: &str) -> RedisResult<()> {
            // Simply return success for tests
            Ok(())
        }

        fn set_ex(&mut self, _key: &str, _value: &str, _seconds: u64) -> RedisResult<()> {
            // Simply return success for tests
            Ok(())
        }

        fn del(&mut self, key: &str) -> RedisResult<i32> {
            match self.del_results.get(key) {
                Some(result) => Ok(*result),
                None => Err(RedisError::from((
                    redis::ErrorKind::TypeError,
                    "Key not configured in mock",
                ))),
            }
        }

        fn keys(&mut self, pattern: &str) -> RedisResult<Vec<String>> {
            match self.keys_results.get(pattern) {
                Some(result) => Ok(result.clone()),
                None => Err(RedisError::from((
                    redis::ErrorKind::TypeError,
                    "Pattern not configured in mock",
                ))),
            }
        }
    }

    // We'll need to patch the RedisClient to use our mock
    // This avoids the trait/struct mismatch issue
    struct TestRedisClient {
        connection: Arc<Mutex<MockConnection>>,
        config: RedisConfig,
    }

    impl TestRedisClient {
        // Implement the same methods as RedisClient but using our MockConnection
        fn get<T: std::string::ToString + std::fmt::Display>(
            &self,
            key: T,
        ) -> RedisResult<Option<String>> {
            let key_str = key.to_string();
            let mut connection = self.connection.lock().map_err(|e| {
                RedisError::from(std::io::Error::other(format!(
                    "Failed to acquire Redis connection lock: {}",
                    e
                )))
            })?;

            let exists = connection.exists(&key_str)?;

            if !exists {
                return Ok(None);
            }

            let value = connection.get(&key_str)?;
            Ok(Some(value))
        }

        fn set<T: std::string::ToString, V: std::string::ToString>(
            &self,
            key: T,
            value: V,
            expiry_secs: Option<u64>,
        ) -> RedisResult<()> {
            let key_str = key.to_string();
            let value_str = value.to_string();
            let mut connection = self.connection.lock().map_err(|e| {
                RedisError::from(std::io::Error::other(format!(
                    "Failed to acquire Redis connection lock: {}",
                    e
                )))
            })?;

            match expiry_secs {
                Some(secs) => connection.set_ex(&key_str, &value_str, secs),
                None => connection.set(&key_str, &value_str),
            }
        }

        fn delete<T: std::string::ToString>(&self, key: T) -> RedisResult<bool> {
            let key_str = key.to_string();
            let mut connection = self.connection.lock().map_err(|e| {
                RedisError::from(std::io::Error::other(format!(
                    "Failed to acquire Redis connection lock: {}",
                    e
                )))
            })?;

            let deleted = connection.del(&key_str)?;
            Ok(deleted > 0)
        }

        fn keys<T: std::string::ToString>(&self, pattern: T) -> RedisResult<Vec<String>> {
            let pattern_str = pattern.to_string();
            let mut connection = self.connection.lock().map_err(|e| {
                RedisError::from(std::io::Error::other(format!(
                    "Failed to acquire Redis connection lock: {}",
                    e
                )))
            })?;

            connection.keys(&pattern_str)
        }

        fn exists<T: std::string::ToString>(&self, key: T) -> RedisResult<bool> {
            let key_str = key.to_string();
            let mut connection = self.connection.lock().map_err(|e| {
                RedisError::from(std::io::Error::other(format!(
                    "Failed to acquire Redis connection lock: {}",
                    e
                )))
            })?;

            connection.exists(&key_str)
        }

        fn get_config(&self) -> &RedisConfig {
            &self.config
        }
    }

    // Now the tests

    #[test]
    fn test_get_existing_key() {
        // Create a mock connection with predefined behavior
        let mock_conn = MockConnection::new()
            .with_exists("test_key", true)
            .with_get("test_key", "test_value");

        // Create the Redis client with the mock connection
        let client = create_test_client(mock_conn);

        // Test the get method
        let result = client.get("test_key");

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), Some("test_value".to_string()));
    }

    #[test]
    fn test_get_non_existing_key() {
        // Create a mock connection with predefined behavior
        let mock_conn = MockConnection::new().with_exists("non_existing_key", false);

        // Create the Redis client with the mock connection
        let client = create_test_client(mock_conn);

        // Test the get method
        let result = client.get("non_existing_key");

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), None);
    }

    #[test]
    fn test_set_without_expiry() {
        // The simplified implementation of set always returns success
        let mock_conn = MockConnection::new();

        // Create the Redis client with the mock connection
        let client = create_test_client(mock_conn);

        // Test the set method
        let result = client.set("test_key", "test_value", None);

        assert!(result.is_ok());
    }

    #[test]
    fn test_set_with_expiry() {
        // The simplified implementation of set_ex always returns success
        let mock_conn = MockConnection::new();

        // Create the Redis client with the mock connection
        let client = create_test_client(mock_conn);

        // Test the set method with expiration
        let result = client.set("test_key", "test_value", Some(60));

        assert!(result.is_ok());
    }

    #[test]
    fn test_delete_existing_key() {
        // Create a mock connection with predefined behavior
        let mock_conn = MockConnection::new().with_del("test_key", 1);

        // Create the Redis client with the mock connection
        let client = create_test_client(mock_conn);

        // Test the delete method
        let result = client.delete("test_key");

        assert!(result.is_ok());
        assert!(result.unwrap());
    }

    #[test]
    fn test_delete_non_existing_key() {
        // Create a mock connection with predefined behavior
        let mock_conn = MockConnection::new().with_del("non_existing_key", 0);

        // Create the Redis client with the mock connection
        let client = create_test_client(mock_conn);

        // Test the delete method
        let result = client.delete("non_existing_key");

        assert!(result.is_ok());
        assert!(!result.unwrap());
    }

    #[test]
    fn test_keys() {
        // Create a mock connection with predefined behavior
        let mock_conn = MockConnection::new()
            .with_keys("test*", vec!["test1".to_string(), "test2".to_string()]);

        // Create the Redis client with the mock connection
        let client = create_test_client(mock_conn);

        // Test the keys method
        let result = client.keys("test*");

        assert!(result.is_ok());
        assert_eq!(
            result.unwrap(),
            vec!["test1".to_string(), "test2".to_string()]
        );
    }

    #[test]
    fn test_exists_true() {
        // Create a mock connection with predefined behavior
        let mock_conn = MockConnection::new().with_exists("existing_key", true);

        // Create the Redis client with the mock connection
        let client = create_test_client(mock_conn);

        // Test the exists method
        let result = client.exists("existing_key");

        assert!(result.is_ok());
        assert!(result.unwrap());
    }

    #[test]
    fn test_exists_false() {
        // Create a mock connection with predefined behavior
        let mock_conn = MockConnection::new().with_exists("non_existing_key", false);

        // Create the Redis client with the mock connection
        let client = create_test_client(mock_conn);

        // Test the exists method
        let result = client.exists("non_existing_key");

        assert!(result.is_ok());
        assert!(!result.unwrap());
    }

    #[test]
    fn test_get_config() {
        // Create configuration
        let config = RedisConfig {
            host: "test-host".to_string(),
            port: 6380,
            username: Some("testuser".to_string()),
            password: Some("testpass".to_string()),
            database: 2,
            timeout: 60,
        };

        // Create Redis client
        let mock_conn = MockConnection::new();
        let client = TestRedisClient {
            connection: Arc::new(Mutex::new(mock_conn)),
            config: config.clone(),
        };

        // Test the get_config method
        let result = client.get_config();

        assert_eq!(result.host, "test-host");
        assert_eq!(result.port, 6380);
        assert_eq!(result.username, Some("testuser".to_string()));
        assert_eq!(result.password, Some("testpass".to_string()));
        assert_eq!(result.database, 2);
        assert_eq!(result.timeout, 60);
    }

    // Helper function to create a test Redis client with a mock connection
    fn create_test_client(mock_conn: MockConnection) -> TestRedisClient {
        let config = RedisConfig::default();

        // Create a real client but inject our mock connection
        let _client = Client::open(format!("redis://{}:{}", config.host, config.port))
            .expect("Failed to create Redis client");

        TestRedisClient {
            connection: Arc::new(Mutex::new(mock_conn)),
            config,
        }
    }

    // The following test simulates a poisoned mutex to test error handling
    #[test]
    fn test_get_connection_error() {
        use std::sync::{Arc, Mutex};
        use std::thread;

        // Create a mock connection
        let connection = Arc::new(Mutex::new(MockConnection::new()));

        // Poison the mutex by panicking while holding the lock
        let connection_clone = Arc::clone(&connection);
        let handle = thread::spawn(move || {
            let _lock = connection_clone.lock().unwrap();
            panic!("Intentionally poisoning the mutex");
        });

        // Wait for the thread to complete and the mutex to be poisoned
        let _ = handle.join();

        // Create client with poisoned mutex
        let config = RedisConfig::default();
        let client = TestRedisClient { connection, config };

        // Test the get method - should fail due to poisoned mutex
        let result = client.get("test_key");
        assert!(result.is_err());
    }
}