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

[MainThreadFact] to execute tests on main thread on macos #58

Closed
SupinePandora43 opened this issue Jul 3, 2022 · 6 comments
Closed
Milestone

Comments

@SupinePandora43
Copy link

SupinePandora43 commented Jul 3, 2022

Hi! I have a native library that requires to be interacted with only from main thread on macos. Can this project provide a way to do it?

Expected result

public class MainThreadTests
{
    [MainThreadFact]
    public void TestIt()
    {
        Assert.Equal(1, Thread.CurrentThread.ManagedThreadId);
    }
}
@AArnott
Copy link
Owner

AArnott commented Jul 3, 2022

Most of the attributes in this library are Windows-specific. But UIFact might work for you. It is OS-agnostic, but really the only thing it does is keep the test running on one particular thread even across async awaits.

I'm a bit surprised to hear of a requirement to only be accessed from the "main thread", since in Windows this is virtually indistinguishable from any other thread. Most thread affinitized libraries I see care about either of two other things:

  1. They are not thread-safe, and therefore should only be accessed from one thread, but the particular thread chosen doesn't matter.
  2. They access the UI, so they need to only be accessed on the UI thread.

If your native library actually has one of these two common requirements, I think [UIFact] may work for you. But I'm not sure.

If you try it and find that it's insufficient, and are willing to send a PR to this library to add a new attribute to do what macos requires, comment back on this issue with your proposal and I may take a PR.

@SupinePandora43
Copy link
Author

i tried
I'll make this issue a feature request then

@SupinePandora43 SupinePandora43 changed the title [Q] Does it use main thread? [MainThreadFact] to execute tests on main thread Jul 3, 2022
@AArnott
Copy link
Owner

AArnott commented Jul 3, 2022

I'll need to know what you're requesting then. What is it about UIFact that isn't enough for your native library, for example? I don't have a mac, so this isn't a feature I'm likely to be able to do myself without a precise spec. Can you offer one?

@AArnott AArnott changed the title [MainThreadFact] to execute tests on main thread [MainThreadFact] to execute tests on main thread on macos Jul 3, 2022
@SupinePandora43
Copy link
Author

Unfortunately [UIFact] isn't enough. What di you mean by spec? Main thread is main thread, but i guess it is unavailable to be modified in xunit?

@AArnott
Copy link
Owner

AArnott commented Jul 4, 2022

In Windows, the main thread is the entrypoint thread. No test runner I've seen exposes that for tests. The main thread just sleeps while tests are running (waking up to schedule them, perhaps).
I don't know what it's like on a mac, if macos defines main thread differently. But I suspect the xunit runner on macos is the same, in that it's impossible to get the test to actually run on the entrypoint thread.

So to make progress on this, I think you'll need to research what this native module you're using really means by the main thread. If it means it literally checks to make sure its code is running on the entrypoint thread, there's nothing that can be done there except switch your testing from Xunit to a hand-written exe that runs your own methods as tests. If there are other more flexible requirements about the thread it's on, we may be able to do something here.

@AArnott
Copy link
Owner

AArnott commented Jun 5, 2023

Closed by #68

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