diff --git a/hitbox-tarantool/Cargo.toml b/hitbox-tarantool/Cargo.toml index 317a9f2..c9be935 100644 --- a/hitbox-tarantool/Cargo.toml +++ b/hitbox-tarantool/Cargo.toml @@ -28,3 +28,4 @@ tokio = { version = "1", features = [ "rt-multi-thread", ] } testcontainers = "0.14" +chrono = "0.4" diff --git a/hitbox-tarantool/src/backend.rs b/hitbox-tarantool/src/backend.rs index 0770ca5..911ca5b 100644 --- a/hitbox-tarantool/src/backend.rs +++ b/hitbox-tarantool/src/backend.rs @@ -147,7 +147,10 @@ impl CacheBackend for TarantoolBackend { " local space_name = ... box.schema.space.create(space_name, { if_not_exists = true }) - box.space[space_name]:create_index('primary', { if_not_exists = true }) + box.space[space_name]:create_index('primary', { + parts = { { 1, 'string' } }, + if_not_exists = true, + }) if not _G.__hitbox_cache_fiber then _G.__hitbox_cache_fiber = require('fiber').create(function() diff --git a/hitbox-tarantool/tests/integration_test.rs b/hitbox-tarantool/tests/integration_test.rs index 657c79e..4ef4b5c 100644 --- a/hitbox-tarantool/tests/integration_test.rs +++ b/hitbox-tarantool/tests/integration_test.rs @@ -1,8 +1,10 @@ -use std::{collections::HashMap, vec}; - -use hitbox_backend::CacheBackend; +use async_trait::async_trait; +use chrono::Utc; +use hitbox_backend::{CacheBackend, CacheableResponse, CachedValue}; use hitbox_tarantool::TarantoolBackendBuilder; -use rusty_tarantool::tarantool::ClientConfig; +use rusty_tarantool::tarantool::{ClientConfig, ExecWithParamaters}; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; use testcontainers::{clients, core::WaitFor, Image}; #[derive(Debug)] @@ -80,3 +82,60 @@ async fn test_start() { .unwrap(); assert!(fiber_exists.0); } + +#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)] +struct Test { + a: i32, + b: String, +} + +#[async_trait] +impl CacheableResponse for Test { + type Cached = Self; + + async fn into_cached(self) -> Self::Cached { + self + } + async fn from_cached(cached: Self::Cached) -> Self { + cached + } +} + +impl Test { + pub fn new() -> Self { + Self { + a: 42, + b: "nope".to_owned(), + } + } +} + +#[tokio::test] +async fn test_set() { + let docker = clients::Cli::default(); + let container = docker.run(Tarantool::default()); + let port = container + .ports() + .map_to_host_port_ipv4(3301) + .unwrap() + .to_string(); + let backend = TarantoolBackendBuilder::default() + .port(port.clone()) + .build(); + backend.start().await.unwrap(); + + let key = "test_key".to_owned(); + let value = CachedValue::new(Test::new(), Utc::now()); + backend.set::(key.clone(), value, None).await.unwrap(); + + let tarantool = + ClientConfig::new(format!("{}:{}", "127.0.0.1", port), "hitbox", "hitbox").build(); + + tarantool + .prepare_fn_call("box.space.hitbox_cache:get") + .bind_ref(&("test_key",)) + .unwrap() + .execute() + .await + .unwrap(); +}