Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 428021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:38:03+00:00 2026-05-12T19:38:03+00:00

lets say i have a background worker in a class that perform db query

  • 0

lets say i have a background worker in a class that perform db query in a background thread.

i wanna test this class

so i mock my db and return some collection so far so good,
make sure my background worker called the do work
and than i wanna make sure that the finish also happened.

I’ve noticed that the test pass and fail randomly (i think it has something to do with threads)

any suggestions

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T19:38:03+00:00Added an answer on May 12, 2026 at 7:38 pm

    You may have a race condition between the background thread and the asserts/verifies.

    For example:

    [Test]
    public void TestWithRaceCondition()
    {
        bool called = false;
        new Thread(() => called = true).Start();
        Assert.IsTrue(called);
    }
    

    The thread doesn’t necessarily end before the asserts, sometimes it will and sometimes it won’t. A solution to this case is to join the background thread:

    [Test]
    public void TestWithoutRaceCondition()
    {
        bool called = false;
        var thread = new Thread(() => called = true);
        thread.Start();
        thread.Join()
        Assert.IsTrue(called);
    }
    

    One way to check if it’s a race condition is to delay the test thread (call Thread.Sleep for long time just before the assert) and if the test stops failing that’s good indication for race condition.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 244k
  • Answers 244k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Couple of things you can do. 1) Change your div… May 13, 2026 at 7:58 am
  • Editorial Team
    Editorial Team added an answer Your design is far too specific. You will need to… May 13, 2026 at 7:58 am
  • Editorial Team
    Editorial Team added an answer Just open two files: $handle1 = fopen($filename1, "w+"); $handle2 =… May 13, 2026 at 7:58 am

Related Questions

Quick background: I'm programming in PHP, I have a domain model with a separate
I'm having a problem designing part of my program (not writing it, for once!).
Lets just say you have a simple operation that runs on a background thread.
I am writing a footer div that displays info from the database. The footer

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.