Nazariglez / notan

Cross-platform multimedia layer
https://nazariglez.github.io/notan-web/
Apache License 2.0
802 stars 55 forks source link

Tests that make a notan instance fail, winit "Initializing the event loop outside of the main thread is a significant cross-platform compatibility hazard" #301

Closed abstralexis closed 3 months ago

abstralexis commented 10 months ago

Hello, I am trying to write a test for a struct that I have created - it will eventually be used for loading textures for an engine that I am making. Thus, I am trying to test the initialisation function for sanity. However, I am getting a test fail.

//! This is the library for texture operations as well
//! as the serialisation structures for the textures.

/// Constant that represents the missing texture path relative
/// to this file. The missing texture is used to represent when a
/// texture is not found by the game engine.
pub const MISSING_TEX_PATH: &str = "./assets/missingtexture.png";

use notan::app::Graphics;
use notan::prelude::{Texture, TextureBuilder};
use std::collections::HashMap;
use std::fs::read;

#[allow(dead_code)]
pub struct TextureLoader {
    textures: HashMap<String, Texture>,
}
impl TextureLoader {
    pub fn new(gfx: &mut Graphics) -> Self {
        let missingtex = TextureBuilder::new(gfx)
            .from_image(
                read(MISSING_TEX_PATH)
                    .expect("Texture Loading Failed")
                    .as_slice(),
            )
            .build()
            .expect("Texture Building Failed");

        let mut textures = HashMap::new();
        textures.insert("missingtex".to_owned(), missingtex);

        TextureLoader { textures }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use notan::{draw::DrawConfig, prelude::*};

    #[test]
    fn panic_test() {
        notan::init()
            .add_config(DrawConfig)
            .draw(draw)
            .build()
            .unwrap();

        fn draw(_app: &mut App, gfx: &mut Graphics) {
            TextureLoader::new(gfx);
        }
    }
}

When I run with cargo test, I get a test failure with the message:

test tests::panic_test ... FAILED

failures:

---- tests::panic_test stdout ----
thread 'tests::panic_test' panicked at 'Initializing the event loop outside of the main thread is a significant cross-platform compatibility hazard. If you absolutely need to create an EventLoop on a different thread, you can use the `EventLoopBuilderExtUnix::any_thread` function.', /home/ubuntu/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.28.7/src/platform_impl/linux/mod.rs:697:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

failures:
    tests::panic_test

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

Is there a known way to get around this to test functions that act upon structs given by a notan instance?

Nazariglez commented 9 months ago

Hello! Winit (the window crate we use) needs to run in the main thread. But to run test we don't need winit, you can use notan without winit for testing. Just use the latest version on github (it has a small fix that avoids shader compilation without backend) and run your test with --no-default-features to remove the backend feature. You may want to add your own set of features, like draw, log etc...

abstralexis commented 9 months ago

Hello, I tried this and I still got the same error. I tried putting the following in my Cargo.toml:

[patch.crates-io]
notan = { version = "0.11.0", default-features = false, features = ["draw"] }

Like shown in https://doc.rust-lang.org/cargo/reference/overriding-dependencies.html yet I'm still getting the same error. Would there be any way to set up Graphics without a notan build that does not require a backend?

Nazariglez commented 8 months ago

Hmm, is there any reason to use [patch.crates-io ] instead of just cargo dependencies? I think I am missing some info here.

I think a cargo config like this:

[dependencies]
notan = { version = "0.11.0", git = "https://github.com/Nazariglez/notan", branch = "develop" }

[features]
testing = ["notan/log", "notan/draw", "notan/random", "notan/glsl-to-spirv"]

Will work, it will use default features when using the regular build command, and then we can do cargo test with no-default-features and passing features=testing to include the basic things you need.

Nazariglez commented 3 months ago

Closing, feel free to reopen if there is still something to discuss. Thanks!