testfx icon indicating copy to clipboard operation
testfx copied to clipboard

Accessing a unique worker ID during runtime

Open mills-andrew opened this issue 4 years ago • 8 comments

I require the ability to access the current workers ID at runtime. Something like the Thread.CurrentThread.ManagedThreadID feature. I want the ability to say Worker.GetWorkerID or something like that. Does anyone know if there is a feature like this?

mills-andrew avatar Aug 08 '19 12:08 mills-andrew

There isn't any such feature right now.

Why wouldn't Thread.CurrentThread.ManagedThreadID itself work for you? And i'm guessing you need this when in-assembly parallelism is enabled?

ShreyasRmsft avatar Aug 09 '19 08:08 ShreyasRmsft

Thats a shame :( and Yes I have tried this method, but the thread ID changes randomly mid run. See https://stackoverflow.com/questions/57414909/managedthreadid-changing-at-runtime

I have tried using the thread ID. Maybe i am doing something wrong.

mills-andrew avatar Aug 09 '19 11:08 mills-andrew

ive narrowed down the threadID issue. If I am creating a thread under the classInitialize but trying to destroy the thread under class cleanup. they work off different threads... wtf?

mills-andrew avatar Aug 09 '19 12:08 mills-andrew

[2019-08-09 08:21:56] AssemblyInitialize: 12 [2019-08-09 08:21:56] ClassInitialize: 12 [2019-08-09 08:21:56] TestMethod: 12 [2019-08-09 08:21:56] ClassCleanUp: 4 [2019-08-09 08:21:56] AssemblyCleanup: 4

Just simply logging those threads during run time... why would they be changing. Any idea why this is happening?

 <!-- MSTest adapter -->
  <MSTest>
    <Parallelize>
      <Workers>1</Workers>
      <Scope>ClassLevel</Scope>
    </Parallelize>
  </MSTest>

mills-andrew avatar Aug 09 '19 12:08 mills-andrew

@KitoCoding yeah so class cleanup and assembly cleanup are sorta invoked at the very end. There is a related issue where someone was asking that these be done immediately after running all tests of a class/assembly.

Since we run these at the end i'm guessing different threads run the cleanup methods.

ShreyasRmsft avatar Aug 12 '19 06:08 ShreyasRmsft

Class cleanup and assembly cleanup are common for a bunch of tests so it is quite likely that even if they are called immediately after all tests of a class/assembly finish it will likely be picked up by a random thread.

ShreyasRmsft avatar Aug 12 '19 06:08 ShreyasRmsft

Yeah it was probably me that try to start that thread lol. Ms test is prioritized for some reason to run one worker per test method. However you add the ability to have one worker pertest class. but the functionality around it as in this case where I need the test cleanup methods to run on the same thread, break. or when a test clean up method is being called at the very end when I need it to run at the end of a workers lifetime.

mills-andrew avatar Aug 12 '19 16:08 mills-andrew

@KitoCoding fair ask, but we will be prioritizing this based on popularity of the feature request.

ShreyasRmsft avatar Aug 13 '19 12:08 ShreyasRmsft