When starting an Actor 'spawn_local
called from outside of a task::LocalSet
'
#2372
-
I am trying to learn how actors work by creating database connection actor. But I have trouble creating an Actor even without a database in mind
use actix::prelude::*;
use backoff::ExponentialBackoff;
pub struct Database {
addr: String,
backoff: ExponentialBackoff,
//client: Option<Some kind of client>
}
impl Database {
pub fn start<S: Into<String>>(addr: S) -> Addr<Database> {
let addr = addr.into();
let mut backoff = ExponentialBackoff::default();
backoff.max_elapsed_time = None;
Context::new().run(Database {
addr,
backoff,
// client: None,
})
}
}
impl Actor for Database {
type Context = Context<Self>;
fn started(&mut self, ctx: &mut Context<Self>) {
dbg!("Create client!");
// TODO
}
}
#[cfg(test)]
mod tests {
use super::*;
use actix_web::{get, post, test, App, Responder};
use actix_web::{web, HttpResponse};
#[get("/{number}")]
pub async fn test_endpoint(
number: web::Path<u64>,
database: web::Data<Database>,
) -> impl Responder {
// Do things with database
// TODO
return HttpResponse::Ok().json(number.0);
}
#[actix_rt::test]
async fn it_works() {
let mut app = test::init_service(
App::new()
.data(Database::start("ADDRESS"))
.service(test_endpoint),
)
.await;
// Make some request to test endpoint
let req = test::TestRequest::default().uri("/123").to_request();
let response: u64 = test::read_response_json(&mut app, req).await;
assert_eq!(response, 123);
}
} When running the test, I get:
How I understand it is that we spawn threads outside some local set (?) and that is not allowed. But I don't see how I could change this. I have been somewhat following the RedisActor implementation I also tried removing the custom start method and using the default one provided with Actor, but same result. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
I did not the understand the concept of the Actor system good enough. The endpoint should consume |
Beta Was this translation helpful? Give feedback.
-
https://crates.io/crates/actix/0.12.0/dependencies actix 0.12 depend on actix-rt 2.0. Instead you can use actix 0.10 |
Beta Was this translation helpful? Give feedback.
-
This error happened for me, too. The error was that I used Here are my versions:
|
Beta Was this translation helpful? Give feedback.
https://crates.io/crates/actix/0.12.0/dependencies
actix 0.12 depend on actix-rt 2.0.
Instead you can use actix 0.10