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 6730087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:21:59+00:00 2026-05-26T10:21:59+00:00

Imagine a function like this: private static ConcurrentList<object> list = new ConcurrentList<object>(); public void

  • 0

Imagine a function like this:

private static ConcurrentList<object> list = new ConcurrentList<object>();
public void Add(object x)
{
   Task.Factory.StartNew(() =>
   {
      list.Add(x); 
   }
}

I don’t care WHEN exactly the fentry is added to the list, but i need it to be added in the end ( obviously 😉 )

I don’t see a way to properly unittest stuff like this without returning any callback-handler or sth. and therefor adding logic that’s not required for the program

How would you do it?

  • 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-26T10:21:59+00:00Added an answer on May 26, 2026 at 10:21 am

    One way to do this is to make your type configurable such that it takes a TaskScheduler instance.

    public MyCollection(TaskScheduler scheduler) {
      this.taskFactory = new TaskFactory(scheduler);
    }
    
    public void Add(object x) {
      taskFactory.StartNew(() => {
        list.Add(x);
      });
    }
    

    Now in your unit tests what you can do is create a testable version of TaskScheduler. This is an abstract class which is designed to be configurable. Simple have the schedule function add the items into a queue and then add a function to manually do all of the queue items “now”. Then your unit test can look like this

    var scheduler = new TestableScheduler();
    var collection = new MyCollection(scehduler);
    collection.Add(42);
    scheduler.RunAll();
    Assert.IsTrue(collection.Contains(42));
    

    Example implementation of TestableScehduler

    class TestableScheduler : TaskScheduler {
      private Queue<Task> m_taskQueue = new Queue<Task>();
    
      protected override IEnumerable<Task> GetScheduledTasks() {
        return m_taskQueue;
      }
    
      protected override void QueueTask(Task task) {
        m_taskQueue.Enqueue(task);
      }
    
      protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued) {
        task.RunSynchronously();
      }
    
      public void RunAll() {
        while (m_taskQueue.Count > 0) {
          m_taskQueue.Dequeue().RunSynchronously();
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine I have a template function like this: template<typename Iterator> void myfunc(Iterator a, typename
Let's imagine something like this: class MyTable extends Doctrine_Table { public function construct() {
Imagine such situation that I have a function like this: Object f() { Object
Imagine this function: void SoundManager::playSource(ALuint sourceID, float offset) { alSourceStop(sourceID); ALint iTotal = 0;
Let's imagine that we have object Animal $.Animal = function(options) { this.defaults = {
Imagine this scenario: class Page {} class Book { private $pages = array(); public
Lets imagine function like this: function foo(x) { x += '+'; return x; }
Imagine I have this code: var myFunc1 = function(event) { alert(1); } var myFunc2
Imagine I have this object (written with Ruby literals) stored in a MongoDB: {tags
Imagine I have an JS array like this: var a = [1, 2, 3,

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.