Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
AlexisComix opened this issue Dec 7, 2023 · 4 comments

Comments

@AlexisComix
Copy link

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?

@AlexisComix AlexisComix changed the title Tests that make a notan instance fail, winit "Initialising Initializing the event loop outside of the main thread is a significant cross-platform compatibility hazard" Tests that make a notan instance fail, winit "Initializing the event loop outside of the main thread is a significant cross-platform compatibility hazard" Dec 7, 2023
@Nazariglez
Copy link
Owner

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...

@AlexisComix
Copy link
Author

AlexisComix commented Dec 10, 2023

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
Copy link
Owner

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants