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

  • SEARCH
  • Home
  • 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 8060259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:55:38+00:00 2026-06-05T09:55:38+00:00

Is it possible to reset the state of dispatch_once code in a unit test

  • 0

Is it possible to reset the state of dispatch_once code in a unit test tearDown?

I think it would be nice if our unit tests could run from a really clean state, but we are struggling with dispatch_once and some singletons made with dispatch once.

  • 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-05T09:55:40+00:00Added an answer on June 5, 2026 at 9:55 am

    I should note first that this isn’t a good thing to do in any situation other than testing; even then, proceed with care — AliSoftware provides some details and example code in comments below. See also the interesting answers at Can I declare a dispatch_once_t predicate as a member variable instead of static?, including some important information from the horse’s mouth.

    dispatch_once_t is a typedefd long. Its false value is 0. If you reset that flag to 0, dispatch_once() will run again. Your problem is “just” how to change the value of a static variable from another compilation unit. For this, I think you need a debug/unit test hook, like so:

    MakeWhoopie.h

    #import <Foundation/Foundation.h>
    
    void makeWhoopie(void);
    
    #ifdef DEBUG
    void resetDispatchOnce(void);
    #endif
    

    MakeWhoopie.m

    #include "MakeWhoopie.h"
    
    static dispatch_once_t * once_token_debug;
    
    void makeWhoopie(void)
    {
    
        static dispatch_once_t once_token;
        once_token_debug = &once_token;    // Store address of once_token
                                           // to access it in debug function.
        dispatch_once(&once_token, ^{
            NSLog(@"That's what you get, folks.");
        });
    
        NSLog(@"Making whoopie.");
    }
    
    #ifdef DEBUG
    void resetDispatchOnce(void)
    {
        *once_token_debug = 0;
    }
    #endif
    

    (You could also move once_token up to file level and change it directly.)

    Trying this out:

    #import <Foundation/Foundation.h>
    #import "MakeWhoopie.h"
    
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
    
            makeWhoopie();
            makeWhoopie();
            resetDispatchOnce();
            makeWhoopie();
        }
        return 0;
    }
    

    Results in:

    2012-06-07 18:45:28.134 ResetDispatchOnce[8628:403] That’s what you get, folks.
    2012-06-07 18:45:28.163 ResetDispatchOnce[8628:403] Making whoopie.
    2012-06-07 18:45:28.164 ResetDispatchOnce[8628:403] Making whoopie.
    2012-06-07 18:45:28.165 ResetDispatchOnce[8628:403] That’s what you get, folks.
    2012-06-07 18:45:28.165 ResetDispatchOnce[8628:403] Making whoopie.

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

Sidebar

Related Questions

Possible Duplicate: Where do the Python unit tests go? Are unit tests kept in
is it possible to reset to original state after changing class and applying the
I would like to unit test a method on a class I have made,
Is it possible to reset a activity to its normal state when a user
I know it's possible to reset the My.Settings with Reset() method. Is there anyway
Is there any way to (robustly) reset any possible :after and :before CSS rules
Is it possible to reset an instance of R? Eg. if I have used
In .NET Framework v4.0, is it possible to override the state change of a
I am trying to write a query and would like some help if possible.
Possible Duplicates: What is REST?[closed] Does the WCF REST WebChannelFactory client support REST services

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.