Return Arc<RedisServer>

This commit is contained in:
Temirkhan Myrzamadi 2020-07-04 21:02:07 +06:00
parent 82ade822cf
commit 56dadfbb34
3 changed files with 16 additions and 22 deletions

View file

@ -9,7 +9,6 @@ mod transitions;
use states::*;
use transitions::*;
use std::sync::Arc;
use teloxide::{
dispatching::dialogue::{serializer::Bincode, RedisStorage, Storage},
prelude::*,
@ -54,15 +53,13 @@ async fn run() {
.await
.expect("Something is wrong with the bot!")
},
Arc::new(
// You can also choose serializer::JSON or serializer::CBOR
// All serializers but JSON require enabling feature
// "serializer-<name>", e. g. "serializer-cbor"
// or "serializer-bincode"
RedisStorage::open("redis://127.0.0.1:6379", Bincode)
.await
.unwrap(),
),
// You can also choose serializer::JSON or serializer::CBOR
// All serializers but JSON require enabling feature
// "serializer-<name>", e. g. "serializer-cbor"
// or "serializer-bincode"
RedisStorage::open("redis://127.0.0.1:6379", Bincode)
.await
.unwrap(),
))
.dispatch()
.await;

View file

@ -35,13 +35,13 @@ impl<S> RedisStorage<S> {
pub async fn open(
url: impl IntoConnectionInfo,
serializer: S,
) -> Result<Self, RedisStorageError<Infallible>> {
Ok(Self {
) -> Result<Arc<Self>, RedisStorageError<Infallible>> {
Ok(Arc::new(Self {
conn: Mutex::new(
redis::Client::open(url)?.get_async_connection().await?,
),
serializer,
})
}))
}
}

View file

@ -10,25 +10,22 @@ use teloxide::dispatching::dialogue::{
#[tokio::test]
async fn test_redis_json() {
let storage = Arc::new(
RedisStorage::open("redis://127.0.0.1:7777", JSON).await.unwrap(),
);
let storage =
RedisStorage::open("redis://127.0.0.1:7777", JSON).await.unwrap();
test_redis(storage).await;
}
#[tokio::test]
async fn test_redis_bincode() {
let storage = Arc::new(
RedisStorage::open("redis://127.0.0.1:7778", Bincode).await.unwrap(),
);
let storage =
RedisStorage::open("redis://127.0.0.1:7778", Bincode).await.unwrap();
test_redis(storage).await;
}
#[tokio::test]
async fn test_redis_cbor() {
let storage = Arc::new(
RedisStorage::open("redis://127.0.0.1:7779", CBOR).await.unwrap(),
);
let storage =
RedisStorage::open("redis://127.0.0.1:7779", CBOR).await.unwrap();
test_redis(storage).await;
}