Skip to content

Commit

Permalink
Refine CacheDatabaseConfig and fix tests in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Dec 4, 2023
1 parent 0b607ef commit 7576d1f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
20 changes: 18 additions & 2 deletions nautilus_core/infrastructure/src/redis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,8 @@ fn get_redis_url(config: &HashMap<String, Value>) -> String {
fn get_buffer_interval(config: &HashMap<String, Value>) -> Duration {
let buffer_interval_ms = config
.get("buffer_interval_ms")
.map(|v| v.as_u64().unwrap_or(10));
Duration::from_millis(buffer_interval_ms.unwrap())
.map(|v| v.as_u64().unwrap_or(0));
Duration::from_millis(buffer_interval_ms.unwrap_or(0))
}

fn get_trader_key(
Expand Down Expand Up @@ -617,6 +617,22 @@ mod tests {
assert!(key.ends_with(&instance_id.to_string()));
}

#[rstest]
fn test_get_buffer_interval_default() {
let config = HashMap::new();
let buffer_interval = get_buffer_interval(&config);
assert_eq!(buffer_interval, Duration::from_millis(0));
}

#[rstest]
fn test_get_buffer_interval() {
let mut config = HashMap::new();
config.insert("buffer_interval_ms".to_string(), json!(100));

let buffer_interval = get_buffer_interval(&config);
assert_eq!(buffer_interval, Duration::from_millis(100));
}

#[rstest]
fn test_get_collection_key_valid() {
let key = "collection:123";
Expand Down
6 changes: 6 additions & 0 deletions nautilus_trader/cache/database.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ cdef class CacheDatabaseAdapter(CacheDatabaseFacade):
Condition.type(config, CacheDatabaseConfig, "config")
super().__init__(logger, config)

if config.buffer_interval_ms and config.buffer_interval_ms >= 1000:
self._log.warning(
f"High `buffer_interval_ms` at {config.buffer_interval_ms}, "
"recommended range is [10, 100] milliseconds.",
)

# Database keys
self._key_trader = f"{_TRADER}-{trader_id}" # noqa
self._key_general = f"{self._key_trader}:{_GENERAL}:" # noqa
Expand Down
1 change: 1 addition & 0 deletions nautilus_trader/config/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class CacheDatabaseConfig(NautilusConfig, frozen=True):
If database should be flushed on start.
buffer_interval_ms : PositiveInt, optional
The buffer interval (milliseconds) between pipelined/batched transactions.
The recommended range if using buffered pipeling is [10, 100] milliseconds.
use_trader_prefix : bool, default True
If a 'trader-' prefix is applied to keys.
use_instance_id : bool, default False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def setup(self):
def teardown(self):
# Tests will fail if Redis is not flushed on tear down
self.database.flush() # Comment this line out to preserve data between tests for debugging
time.sleep(0.5) # Ensure clean slate
time.sleep(1.0) # Ensure clean slate

@pytest.mark.asyncio
async def test_load_general_objects_when_nothing_in_cache_returns_empty_dict(self):
Expand Down

0 comments on commit 7576d1f

Please sign in to comment.