PHP Classes

File: tests/Integration/TimeoutTest.php

Recommend this page to a friend!
  Packages of Gianfrancesco Aurecchia   OPC UA Client   tests/Integration/TimeoutTest.php   Download  
File: tests/Integration/TimeoutTest.php
Role: Example script
Content type: text/plain
Description: Example script
Class: OPC UA Client
Control devices that support the OPC UA protocol
Author: By
Last change:
Date: 19 days ago
Size: 2,467 bytes
 

Contents

Class file image Download
<?php

declare(strict_types=1);

use
PhpOpcua\Client\ClientBuilder;
use
PhpOpcua\Client\Exception\ConnectionException;
use
PhpOpcua\Client\Tests\Integration\Helpers\TestHelper;
use
PhpOpcua\Client\Types\NodeId;
use
PhpOpcua\Client\Types\StatusCode;

describe('Timeout', function () {

   
it('connects and operates with a custom timeout', function () {
       
$client = null;
        try {
           
$client = (new ClientBuilder())
                ->
setTimeout(10.0)
                ->
connect(TestHelper::ENDPOINT_NO_SECURITY);

           
expect($client->getTimeout())->toBe(10.0);

           
$dataValue = $client->read(NodeId::numeric(0, 2259));
           
expect($dataValue->getStatusCode())->toBe(StatusCode::Good);
        } finally {
           
TestHelper::safeDisconnect($client);
        }
    })->
group('integration');

   
it('connects with a short but sufficient timeout', function () {
       
$client = null;
        try {
           
$client = (new ClientBuilder())
                ->
setTimeout(2.0)
                ->
connect(TestHelper::ENDPOINT_NO_SECURITY);

           
$refs = $client->browse(NodeId::numeric(0, 85));
           
expect($refs)->toBeArray()->not->toBeEmpty();
        } finally {
           
TestHelper::safeDisconnect($client);
        }
    })->
group('integration');

   
it('throws ConnectionException when timeout is too short for unreachable host', function () {
       
$builder = new ClientBuilder();
       
$builder->setTimeout(0.1);

       
$start = microtime(true);
       
$threw = false;
        try {
            @
$builder->connect('opc.tcp://192.0.2.1:4840/UA/TestServer');
        } catch (
ConnectionException) {
           
$threw = true;
        }
       
$elapsed = microtime(true) - $start;

       
expect($threw)->toBeTrue();
       
expect($elapsed)->toBeLessThan(3.0);
    })->
group('integration');

   
it('preserves timeout across multiple operations', function () {
       
$client = null;
        try {
           
$client = (new ClientBuilder())
                ->
setTimeout(10.0)
                ->
connect(TestHelper::ENDPOINT_NO_SECURITY);

           
$client->read(NodeId::numeric(0, 2259));
           
$client->browse(NodeId::numeric(0, 85));
           
$client->read(NodeId::numeric(0, 2259));

           
expect($client->getTimeout())->toBe(10.0);
        } finally {
           
TestHelper::safeDisconnect($client);
        }
    })->
group('integration');

})->
group('integration');