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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:15:59+00:00 2026-06-17T07:15:59+00:00

I am unit testing a new Win8 Store app and noticed a race condition

  • 0

I am unit testing a new Win8 Store app and noticed a race condition I want to avoid. So I am looking for a way to avoid this race condition.

I have a class that when instantiated calls a method to be sure it has a local StorageFolder. My unit test simply instantiates the object and tests if the folder is there. Sometime the folder is not and sometimes it is so I believe this to be a race condition because the CreateFolderAsync is asynchronous (obviously).

public class Class1
{
    StorageFolder _localFolder = null;

    public Class1()
    {
        _localFolder = ApplicationData.Current.LocalFolder;
        _setUpStorageFolders();
    }

    public StorageFolder _LocalFolder
    {
        get
        {
            return _localFolder;
        }

    }


    async void _setUpStorageFolders()
    {
        try
        {
            _localFolder = await _localFolder.CreateFolderAsync("TestFolder", CreationCollisionOption.FailIfExists);

        }
        catch (Exception)
        {
            throw;
        }
    }
}

My Unit Test Looks Like This:

 [TestMethod]
    public void _LocalFolder_Test()
    {
        Class1 ke = new Class1();


        // TODO: Fix Race Condition 
        StorageFolder folder = ke._LocalFolder;

        string folderName = folder.Name;

        Assert.IsTrue(folderName == "TestFolder");

    }
  • 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-06-17T07:16:01+00:00Added an answer on June 17, 2026 at 7:16 am

    As Iboshuizen suggested, I would do this synchronously. This can be done with async, task, and await. There is a gotcha – the setup cannot be done inside the constructor of Class1 because constructors do not support async/ await. Because of this SetUpStorageFolders is now public, and is called from the test method.

    public class Class1
    {
        StorageFolder _localFolder = null;
    
        public Class1()
        {
            _localFolder = ApplicationData.Current.LocalFolder;
                    // call to setup removed here because constructors
                    // do not support async/ await keywords
        }
    
        public StorageFolder _LocalFolder
        {
            get
            {
                return _localFolder;
            }
    
        }
    
          // now public... (note Task return type)
        async public Task SetUpStorageFolders()
        {
            try
            {
                _localFolder = await _localFolder.CreateFolderAsync("TestFolder", CreationCollisionOption.FailIfExists);
    
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
    

    Test:

     // note the signature change here (async + Task)
     [TestMethod]
        async public Task _LocalFolder_Test()
        {
            Class1 ke = new Class1();
            // synchronous call to SetupStorageFolders - note the await
            await ke.SetUpStorageFolders();
    
            StorageFolder folder = ke._LocalFolder;
    
            string folderName = folder.Name;
    
            Assert.IsTrue(folderName == "TestFolder");
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently looking into unit testing for a new application I have to create.
Unit testing is new to me, and I have this error I don't understand.
New to unit testing and have an app that is spread out over several
I'm new with unit testing in visual studio and I want to load a
I'm new in unit testing so may be I do something wrong. I have
I am new to Unit Testing and think I might have dug myself into
I am new to unit testing. I have done unit testing on controller classes
I am very new to PHPUnit and unit-testing, so I have a questing: Can
I am new to unit testing. I have created various tests and when I
I am new to unit testing for web applications I have a function which

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.