Files
namaste/tests/unit/brokerStartupTest.php
gramps 373ebc8c93 Archive: Namaste PHP AMQP framework v1.0 (2017-2020)
952 days continuous production uptime, 40k+ tp/s single node.
Original corpo Bitbucket history not included — clean archive commit.
2026-04-05 09:49:30 -07:00

231 lines
9.0 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: mshallop
* Date: 6/16/17
* Time: 10:19 AM
*/
$_REDIRECT = false;
require(dirname(__DIR__) . '/../config/sneakerstrap.inc'); // and load the namaste environment
use PHPUnit\Framework\TestCase;
class brokerStartupTest extends TestCase
{
protected ?gacBrokerClient $brokerClient;
protected ?gacWorkQueueClient $brokerAdminInClient;
protected static ?gacErrorLogger $logger;
protected static array $brokerList;
/**
* setUpBeforeClass() -- public static unit test method
*
* this method is called first, and once, on execution. It sets up a list of brokers we're going to test.
*
* @author mike@givingassistant.org
* @version 1.0
*
*
* HISTORY:
* ========
* 06-16-17 mks original coding
*
*/
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
// defines the list of brokers we're going to test
static::$brokerList = [BROKER_QUEUE_R, BROKER_QUEUE_W, BROKER_QUEUE_AO, BROKER_QUEUE_AI];
}
/**
* testTimers -- unit test
*
* this method tests the base functionality of the f/w's timer function. We'll make a call to the timer to get
* a non-zero starting time, and test, and then make a second call to the function (which calculates the time
* elapsed) and test that this, too, is non-zero.
*
* Criteria for success: both tested values are non-zero
*
* @author mike@givingassistant.org
* @version 1.0
*
* HISTORY:
* ========
* 06-16-17 mks original coding
*
*/
public function testTimers()
{
/** @noinspection PhpUnusedLocalVariableInspection */
$startTime = floatval(0);
$startTime = gasStatic::doingTime();
$this->assertNotEquals(0, $startTime, __METHOD__ . __LINE__ . sprintf(ERROR_UT_EXPECTING_NON_ZERO_FP, $startTime));
$endTime = gasStatic::doingTime($startTime);
$this->assertNotEquals(0, $startTime, __METHOD__ . __LINE__ . sprintf(ERROR_UT_EXPECTING_NON_ZERO_FP, $endTime));
}
/**
* testNamasteReadBrokerInstantiation()
*
* this unit test creates a connection to the (primary) namaste read broker - which is an RPC broker and makes
* two basic requests -- so the first test is that we can connect to the RMQ queue that the broker is listening
* on.
*
* the second test is a ping request -- where we publish a ping event to the broker and await the response that
* the ping was received and responded to.
*
* the third test is a schema-fetch event request -- we publish a request to the broker requesting the schema
* report for the log template and test that we received the report for the correct template.
*
* @author mike@givingassistant.org
* @version 1.0
*
* HISTORY:
* ========
* 06-16-17 mks original coding
*
*/
public function testNamasteReadBrokerInstantiation()
{
$this->brokerClient = new gacBrokerClient(BROKER_QUEUE_R, basename(__FILE__) . COLON . __METHOD__ . COLON . __LINE__);
$this->assertTrue($this->brokerClient->status, __METHOD__ . __LINE__ . ERROR_BROKER_QUEUE_DECLARE . BROKER_QUEUE_R);
// this is an rpc-broker - so let's ping and get schema...
// set-up the broker request data
$meta = [
META_SYSTEM_NOTES => STRING_ORIGIN_UT,
META_CLIENT => CLIENT_UNIT,
META_SESSION_IP => STRING_SESSION_HOME
];
$request = [
BROKER_REQUEST => BROKER_REQUEST_PING,
BROKER_DATA => [],
BROKER_META_DATA => $meta
];
// publish the AMQP >PING< event request
$response = $this->brokerClient->call(gzcompress(json_encode($request))); // rpc queue
$response = json_decode(gzuncompress($response), true);
$this->assertTrue($response[PAYLOAD_STATUS], __METHOD__ . __LINE__ . ERROR_UT_EXPECTING_TRUE . PAYLOAD_STATUS);
// let's fetch the log table schema
$meta[META_TEMPLATE] = 'Logs';
$request = [
BROKER_REQUEST => BROKER_REQUEST_SCHEMA,
BROKER_DATA => [],
BROKER_META_DATA => $meta
];
$response = $this->brokerClient->call(gzcompress(json_encode($request))); // rpc queue
$response = json_decode(gzuncompress($response), true);
$this->assertTrue($response[PAYLOAD_STATUS], __METHOD__ . __LINE__ . ERROR_UT_BROKER_STATUS);
$this->assertEquals(COLLECTION_MONGO_LOGS, $response[PAYLOAD_RESULTS][TEMPLATE_FEATURE_TITLE], __METHOD__ . __LINE__ . COLON . 'error: expecting ' . COLLECTION_MONGO_LOGS . ' but received: ' . $response[PAYLOAD_RESULTS][TEMPLATE_FEATURE_TITLE]);
// deallocate the broker client
$this->brokerClient->__destruct();
unset($this->brokerClient);
}
/**
* testNamasteWriteBrokerInstantiation()
*
* this unit test creates a connection to the (primary) namaste write broker - which is an RPC broker and makes
* two basic requests -- so the first test is that we can connect to the RMQ queue that the broker is listening
* on.
*
* the second test is a ping request -- where we publish a ping event to the broker and await the response that
* the ping was received and responded to.
*
* @author mike@givingassistant.org
* @version 1.0
*
* HISTORY:
* ========
* 06-19-17 mks original coding
*
*/
public function testNamasteWriteBrokerInstantiation()
{
$this->brokerClient = new gacBrokerClient(BROKER_QUEUE_W, basename(__FILE__) . COLON . __METHOD__ . COLON . __LINE__);
$this->assertTrue($this->brokerClient->status, __METHOD__ . __LINE__ . ERROR_BROKER_QUEUE_DECLARE . BROKER_QUEUE_R);
// this is an rpc-broker - so let's ping and get schema...
// set-up the broker request data
$meta = [
META_SYSTEM_NOTES => STRING_ORIGIN_UT,
META_CLIENT => CLIENT_UNIT,
META_SESSION_IP => STRING_SESSION_HOME
];
$request = [
BROKER_REQUEST => BROKER_REQUEST_PING,
BROKER_DATA => [],
BROKER_META_DATA => $meta
];
// publish the AMQP >PING< event request
$response = $this->brokerClient->call(gzcompress(json_encode($request))); // rpc queue
$response = json_decode(gzuncompress($response), true);
$this->assertTrue($response[PAYLOAD_STATUS], __METHOD__ . __LINE__ . ERROR_UT_EXPECTING_TRUE . PAYLOAD_STATUS);
// deallocate the broker client
$this->brokerClient->__destruct();
unset($this->brokerClient);
}
/**
* testNamasteAdminInBrokerInstantiation()
*
* this unit test creates a connection to the AdminIn broker - which is a fire-n-forget broker and makes
* two basic requests -- so the first test is that we can connect to the RMQ queue that the broker is listening
* on.
*
* the second test is a ping request -- where we publish a ping event to the broker and await the response that
* the ping was received and responded to. Since the broker does not respond to pings, we're just testing that
* the adminIn broker class successfully published a message to the adminIn broker.
*
*
* @author mike@givingassistant.org
* @version 1.0
*
* HISTORY:
* ========
* 06-19-17 mks original coding
*
*/
public function testNamasteAdminInBrokerInstantiation()
{
global $topDir;
$logDir = $topDir . DIR_LOGS;
$successMessage = 'successfully pinged broker: ga_admin_in_queue';
$this->brokerAdminInClient = new gacWorkQueueClient();
$this->assertTrue($this->brokerAdminInClient->status, __METHOD__ . __LINE__ . ERROR_BROKER_QUEUE_DECLARE . BROKER_QUEUE_R);
// this is an rpc-broker - so let's ping and get schema...
// set-up the broker request data
$meta = [
META_SYSTEM_NOTES => STRING_ORIGIN_UT,
META_CLIENT => CLIENT_UNIT,
META_SESSION_IP => STRING_SESSION_HOME
];
$request = [
BROKER_REQUEST => BROKER_REQUEST_PING,
BROKER_DATA => [],
BROKER_META_DATA => $meta
];
// publish the AMQP >PING< event request
$response = $this->brokerAdminInClient->call(gzcompress(json_encode($request))); // rpc queue
$this->assertTrue($response, __METHOD__ . __LINE__ . ERROR_UT_EXPECTING_TRUE . $response);
// grab the last line of the log file which should still be the output from the above request
$file = $logDir . SLASH . 'namaste.log';
$this->assertFileExists($file, ERROR_FILE_404 . $file);
$file = escapeshellarg($file);
sleep(1);
$line = `tail -n 1 $file`;
$this->assertNotFalse(strpos($line, $successMessage), sprintf(ERROR_STRING_404, $successMessage));
// deallocate the broker client
$this->brokerAdminInClient->__destruct();
unset($this->brokerAdminInClient);
}
}