Skip to content

Commit

Permalink
tarantool: add test for set function
Browse files Browse the repository at this point in the history
  • Loading branch information
lowitea committed Aug 6, 2023
1 parent 64f9508 commit ea116bb
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
1 change: 1 addition & 0 deletions hitbox-tarantool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ tokio = { version = "1", features = [
"rt-multi-thread",
] }
testcontainers = "0.14"
chrono = "0.4"
5 changes: 4 additions & 1 deletion hitbox-tarantool/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
67 changes: 63 additions & 4 deletions hitbox-tarantool/tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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::<Test>(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();
}

0 comments on commit ea116bb

Please sign in to comment.