PHP Classes

File: v2/tests/test_multithreading.php

Recommend this page to a friend!
  Classes of Nicola Covolo   Safe IO   v2/tests/test_multithreading.php   Download  
File: v2/tests/test_multithreading.php
Role: Unit test 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/test_multithreading.php
Date: 2 months ago
Size: 2,381 bytes
 

Contents

Class file image Download
<?php

include_once ("../ConcurrentFile.php");
include_once (
"Thread.php");

function
exception_handler($exception)
{
   
file_put_contents("log.txt", "Uncaught exception: " . $exception->getMessage() . "\n");
}

set_exception_handler('exception_handler');

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
read($u)
{
   
$file = new ConcurrentFile("data.txt");
   
$contents = $file->read();
   
$file->close();
    return
$contents;
}

function
write($content)
{
   
$file = new ConcurrentFile("data.txt");
   
$file->write($content)->close();
    return
$content;
}
function
write_slow($content)
{
   
$file = new ConcurrentFile("data.txt");
   
$file->writeLock();
    for(
$i=0;$i<10000;$i++){}
   
$file->write($content)->close();
    return
$content;
}

/*
******************************************************
* Test 1: a writing process followed by a read process.
******************************************************
*/

$content = random_string(30);
$content2 = random_string(30);

$thread_a = new Thread("localhost", 80);
$thread_a->setFunc("write", array($content));
$thread_a->start();

$thread_b = new Thread("localhost", 80);
$thread_b->setFunc("read", array(1));
$thread_b->start();


$returnA = $thread_a->getreturn();
$returnB = $thread_b->getreturn();

echo
"test 1(simple write): ";
if (
$returnA == $returnB)
    echo
"<font color=green>passed</font><br>";
else
    echo
"<font color=red>not passed</font><br>";


/*
***********************************************************
* Test 2: a slow writing process followed by a read process.
* The read process should wait untill the write end.
***********************************************************
*/

$thread_c = new Thread("localhost", 80);
$thread_c->setFunc("write_slow", array($content2));
$thread_c->start();

$thread_d = new Thread("localhost", 80);
$thread_d->setFunc("read", array(1));
$thread_d->start();

$returnC = $thread_c->getreturn();
$returnD = $thread_d->getreturn();

echo
"test 2(slow writing): ";
if (
$returnC == $returnD)
    echo
"<font color=green>passed</font><br>";
else
    echo
"<font color=red>not passed</font><br>";

?>