PHP Classes

File: v2/tests/test4.php

Recommend this page to a friend!
  Classes of Nicola Covolo   Safe IO   v2/tests/test4.php   Download  
File: v2/tests/test4.php
Role: Example script
Content type: text/plain
Description: Unit test script
Class: Safe IO
Access files using locks to prevent corruption
Author: By
Last change: Update of v2/tests/test4.php
Date: 4 months ago
Size: 910 bytes
 

Contents

Class file image Download
<?php
/**
 * This example aiming to test the behavior of CuncurrentFile.php under intensive load
*/
include ("../ConcurrentFile.php");

function
random_string($length)
{
   
$string = "";
    for (
$i = 0; $i <= ($length / 32); $i++)
       
$string .= md5(time() + rand(0, 99));
   
$max_start_index = (32 * $i) - $length;
   
$random_string = substr($string, rand(0, $max_start_index), $length);
    return
$random_string;

}

function
test($content, $n)
{
    for (
$i = 0; $i < $n; $i++)
    {
       
$file = new ConcurrentFile("data.txt");
       
$file->writeLock();
       
$file->write($content);
       
$file->write($content, false);
       
$contents = $file->read();
       
$file->close();

        if (
$contents === ($content . $content))
        {
            echo
"ok<br>";
        }
        else
        {
            echo
"not ok<br>";
        }
    }
}

test(random_string(1000000) , 10);

?>