Skip to content

Commit

Permalink
test: wasm test (#271)
Browse files Browse the repository at this point in the history
* test: wasm test

* ci: run wasm test

* fix: wasm websocket connect

* chore: add logs

* ci: fix
  • Loading branch information
appflowy authored Jan 23, 2024
1 parent 3377e66 commit a318d2f
Show file tree
Hide file tree
Showing 29 changed files with 378 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,15 @@ jobs:
- name: Run tests
run: |
cargo test
- name: Install Node.js
uses: actions/setup-node@v2
with:
node-version: '14'

- name: Run WASM tests
working-directory: ./libs/wasm-test
run: |
cargo install wasm-pack
wasm-pack test --headless --firefox --features="wasm_test"
79 changes: 67 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion libs/client-api-test-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@ uuid = "1.6.1"
lazy_static = "1.4.0"
dotenv = "0.15.0"
reqwest = "0.11.23"
gotrue.workspace = true
gotrue.workspace = true

[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys = { version = "0.3", features = ["console"] }

[features]
wasm_test = []
10 changes: 10 additions & 0 deletions libs/client-api-test-util/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::borrow::Cow;
use std::env;
use tracing::warn;

#[cfg(not(feature = "wasm_test"))]
lazy_static! {
pub static ref LOCALHOST_URL: Cow<'static, str> =
get_env_var("LOCALHOST_URL", "http://localhost:8000");
Expand All @@ -14,6 +15,15 @@ lazy_static! {
get_env_var("LOCALHOST_GOTRUE", "http://localhost:9999");
}

// The env vars are not available in wasm32-unknown-unknown
#[cfg(feature = "wasm_test")]
lazy_static! {
pub static ref LOCALHOST_URL: Cow<'static, str> = Cow::Owned("http://localhost".to_string());
pub static ref LOCALHOST_WS: Cow<'static, str> = Cow::Owned("ws://localhost/ws".to_string());
pub static ref LOCALHOST_GOTRUE: Cow<'static, str> =
Cow::Owned("http://localhost/gotrue".to_string());
}

fn get_env_var<'default>(key: &str, default: &'default str) -> Cow<'default, str> {
dotenv().ok();
match env::var(key) {
Expand Down
22 changes: 8 additions & 14 deletions libs/client-api-test-util/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,12 @@ use dotenv::dotenv;
use lazy_static::lazy_static;
use uuid::Uuid;

#[cfg(not(target_arch = "wasm32"))]
lazy_static! {
pub static ref ADMIN_USER: User = {
dotenv().ok();
User {
email: std::env::var("GOTRUE_ADMIN_EMAIL").unwrap(),
password: std::env::var("GOTRUE_ADMIN_PASSWORD").unwrap(),
}
};
}

#[cfg(target_arch = "wasm32")]
lazy_static! {
pub static ref ADMIN_USER: User = {
dotenv().ok();
User {
email: "[email protected]".to_string(),
password: "password".to_string(),
email: std::env::var("GOTRUE_ADMIN_EMAIL").unwrap_or("[email protected]".to_string()),
password: std::env::var("GOTRUE_ADMIN_PASSWORD").unwrap_or("password".to_string()),
}
};
}
Expand All @@ -39,6 +27,12 @@ pub fn generate_unique_email() -> String {

pub async fn admin_user_client() -> Client {
let admin_client = localhost_client();
#[cfg(target_arch = "wasm32")]
{
let msg = format!("{}", admin_client);
web_sys::console::log_1(&msg.into());
}

admin_client
.sign_in_password(&ADMIN_USER.email, &ADMIN_USER.password)
.await
Expand Down
12 changes: 7 additions & 5 deletions libs/client-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ bytes = "1.5"
uuid = "1.6.1"
futures-util = "0.3.30"
futures-core = "0.3.30"
tokio-retry = "0.3"
parking_lot = "0.12.1"
brotli = "3.4.0"
mime_guess = "2.0.4"
Expand All @@ -45,10 +44,8 @@ database-entity.workspace = true
app-error = { workspace = true, features = ["tokio_error", "bincode_error"] }
scraper = { version = "0.17.1", optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", features = ["js"]}
tokio = { workspace = true, features = ["sync"]}

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio-retry = "0.3"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies.tokio]
workspace = true
Expand All @@ -58,6 +55,11 @@ features = ["sync", "net"]
workspace = true
features = ["tungstenite"]

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen-futures = "0.4.40"
getrandom = { version = "0.2", features = ["js"]}
tokio = { workspace = true, features = ["sync"]}
again = "0.1.2"

[features]
collab-sync = ["collab", "yrs"]
Expand Down
7 changes: 4 additions & 3 deletions libs/client-api/src/collab_sync/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::collab_sync::{SinkConfig, SyncQueue};
use tokio_stream::wrappers::WatchStream;
use tracing::trace;

use crate::platform_spawn;
use crate::ws::{ConnectState, WSConnectStateReceiver};
use yrs::updates::encoder::Encode;

Expand Down Expand Up @@ -63,7 +64,7 @@ where
);

let mut sync_state_stream = WatchStream::new(sync_queue.subscribe_sync_state());
tokio::spawn(async move {
platform_spawn(async move {
while let Some(new_state) = sync_state_stream.next().await {
if let Some(local_collab) = weak_local_collab.upgrade() {
if let Some(local_collab) = local_collab.try_lock() {
Expand All @@ -76,7 +77,7 @@ where
let sync_queue = Arc::new(sync_queue);
let weak_local_collab = collab;
let weak_sync_queue = Arc::downgrade(&sync_queue);
tokio::spawn(async move {
platform_spawn(async move {
while let Ok(connect_state) = ws_connect_state.recv().await {
match connect_state {
ConnectState::Connected => {
Expand Down Expand Up @@ -132,7 +133,7 @@ where
let object_id = self.object.object_id.clone();
let cloned_origin = origin.clone();

tokio::spawn(async move {
platform_spawn(async move {
if let Some(sync_queue) = weak_sync_queue.upgrade() {
let payload = Message::Sync(SyncMessage::Update(update)).encode_v1();
sync_queue
Expand Down
6 changes: 3 additions & 3 deletions libs/client-api/src/collab_sync/sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::collab_sync::pending_msg::{MessageState, PendingMsgQueue};
use crate::collab_sync::{SyncError, SyncObject, DEFAULT_SYNC_TIMEOUT};
use futures_util::SinkExt;

use crate::platform_spawn;
use realtime_entity::collab_msg::{CollabSinkMessage, MsgId};
use tokio::spawn;
use tokio::sync::{mpsc, oneshot, watch, Mutex};
use tokio::time::{interval, Instant, Interval};
use tracing::{debug, error, event, trace, warn};
Expand Down Expand Up @@ -96,7 +96,7 @@ where
let weak_notifier = Arc::downgrade(&notifier);
let (tx, rx) = mpsc::channel(1);
interval_runner_stop_tx = Some(tx);
spawn(IntervalRunner::new(*duration).run(weak_notifier, rx));
platform_spawn(IntervalRunner::new(*duration).run(weak_notifier, rx));
}
Self {
uid,
Expand Down Expand Up @@ -364,7 +364,7 @@ where
}

fn retry_later(weak_notifier: Weak<watch::Sender<bool>>) {
spawn(async move {
platform_spawn(async move {
interval(Duration::from_millis(100)).tick().await;
if let Some(notifier) = weak_notifier.upgrade() {
let _ = notifier.send(false);
Expand Down
Loading

0 comments on commit a318d2f

Please sign in to comment.