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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:20:49+00:00 2026-05-29T09:20:49+00:00

I am working on updating unit tests for my core library, and came across

  • 0

I am working on updating unit tests for my core library, and came across a scenario regarding filelocks (flock), and wanted to see how others implemented these types of unit tests.

Basically I have a utlity File class which will write contents to a file:

class CoreLib_Api_File_Package
{
    public static function write(array $options)
    {
        ...

        if (!$file->flock(LOCK_EX)) {
            throw new CoreLib_Api_Exception('Unable to obtain lock on file');
        }

        ...
    }
}

And my unit test looks like:

public function testWriteException_UnableToSecureLock()
{
    $this->touchFile($this->destFileUri);

    $file = new SplFileObject($this->destFileUri, CoreLib_Api_File::MODE_WRITE);

    $file->flock(LOCK_EX);

    CoreLib_Api_File_Package::write(array('fileUri' => $this->destFileUri, 'content' => $this->testContent, 'mode' => CoreLib_Api_File::MODE_WRITE));
}

As you can see from the test code I am putting an explicit lock on $this->destFileUri before I make the API call to the write() method. What I would expect is that the exception throw new CoreLib_Api_Exception('Unable to obtain lock on file'); to be thrown. Instead, my unit test just hangs indefinitely.

What would be the proper way to test whether a file has a lock?

Thanks in advance.

  • 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-29T09:20:50+00:00Added an answer on May 29, 2026 at 9:20 am

    You should check this comment on the flock docs page.

    Essentially, flock will pause execution until it is able to obtain the lock. Take the following code, which is similar to your situation above:

    $file = 'file.txt';
    
    $first = fopen($file, "w");
    flock($first, LOCK_EX);
    
    $second = fopen($file, "w");
    
    # Your script will pause on the next line until timeout
    # due to the inability to obtain a lock.
    flock($second, LOCK_EX);
    

    However, you can obtain non-blocking locks like this:

    flock($file, LOCK_EX | LOCK_NB);
    

    I am not sure if there are negative implications to locking files this way. Also, while the docs say LOCK_NB does not work on Windows, I just tested it and it worked on my machine. So, you could do something like this in your CoreLib_Api_File_Package::write method:

    class CoreLib_Api_File_Package
    {
        public static function write(array $options)
        {
            if ( ! $file->flock(LOCK_EX | LOCK_NB))
            {
                throw new CoreLib_Api_Exception('Unable to obtain lock on file');
            }
        }
    }
    

    Your current test should pass as-written. Other than that, I can’t think of anything.

    Edit: They should pass as long as you are telling PHPUnit to expect the CoreLib_Api_Exception exception (just pointing it out because you didn’t have it in your example).

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

Sidebar

Related Questions

I am working on updating some of my iOS blog apps to get beyond
I am working on old sites and updating the deprecated php functions. I have
I'm working on creating a self updating application and one issue I'm running into
I'm working on a project updating their WinForms application UI to be more consistent
I'm working on a project that requires a fair bit of inserting/updating rows in
One of my friend is working on issue related to updating expired digital certificates.
I am working on updating a legacy application that is absolutely rife with Singleton
I'm working updating some legacy code that does not properly handle user input. The
I'm working on updating a project which uses the following code to place a
I am working on an ASP.NET project where we are updating all of our

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.