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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:28:31+00:00 2026-06-17T18:28:31+00:00

I want to write unit tests for a subroutine in perl. The subroutine is

  • 0

I want to write unit tests for a subroutine in perl. The subroutine is using multiple threads to do its tasks. So, it first creates some threads and then it waits for them to join.

The problem is that our unit tests run on a server which is not able to run multi-threaded tests, so I need to somehow mock out the thread behavior. Basically I want to override the threads create and join functions such that its not threaded anymore. Any pointers how can I do that and test the code ?

Edit : The server fails to run the threaded code for the following reason:

Devel::Cover does not yet work with threads

  • 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-17T18:28:32+00:00Added an answer on June 17, 2026 at 6:28 pm

    Update: this answer doesn’t solve the OP’s problem as described in the edited question, but it might be useful to someone.

    Perl threads are an interpreter emulation, not an operating system feature. So, they should work on any platform. If your testing server doesn’t support threads, it’s probably for one of these reasons:

    1. Your version of Perl is very old.
    2. Perl was compiled without thread support.
    3. Your testing framework wasn’t created with threaded code in mind.

    The first two could be easily rectified by updating your environment. However, I suspect yours is the third issue.

    I don’t think you should solve this by mocking the thread behavior. This changes the original code too much to be a valid test. And it would be a significant amount of work anyway, so why not direct that effort toward getting a threaded test working?

    The exact issues depend on your code, but probably the issue is that your subroutine starts a thread and then returns, with the thread still running. Then your test framework runs the sub over and over, accumulating a whole bunch of concurrent threads.

    In that case, all you need is a wrapper sub that calls the sub you are testing, and then blocks until the threads are complete. This should be fairly simple. Take a look at threads->list() to see how you can detect running threads. Just have a loop that waits until the threads in question are no longer running before exiting the wrapper sub.

    Here is a simple complete example demonstrating a wrapper sub:

    #!usr/bin/perl
    use strict;
    use warnings;
    use threads;
    
    sub sub_to_test {
    
        threads->create(sub { sleep 5; print("Thread done\n"); threads->detach() });
        return "Sub done\n";
    }
    
    sub wrapper {
        #Get a count of the running threads.
        my $original_running_threads = threads->list(threads::running);
    
        my @results = sub_to_test(@_);
    
        #block until the number of running threads is the same as when we started.
        sleep 1 while (threads->list(threads::running) > $original_running_threads);
    
        return @results;
    }
    
    print wrapper;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing some unit tests in UnitTest++ and want to write a bunch of
Say I want to write some unit tests for the (!!) function. my_prop xs
First,I don't want to do TDD. I'd like to write some code and then
I have some Web API methods that I want to write unit tests for.
I want to write some unit tests for an interceptor that intercepts the Loggable
I've written some abstractions for Crypto.Cipher.AES.encrypt/decrypt , and I want to write unit tests
I want to write unit tests using Apple's default SenTestingKit for the below method:
I do write unit tests while writing APIs and core functionalities. But I want
I have an application where I want to write unit tests to test the
I want to write a few unit tests that do not make any changes

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.