PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/storage-2009-04-14/tests/Microsoft/WindowsAzure/SessionHandlerTest.php

#
PHP | 276 lines | 161 code | 41 blank | 74 comment | 15 complexity | 45c202099249cdb1b7a00ff83209d2e7 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Copyright (c) 2009, RealDolmen
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the name of RealDolmen nor the
  14. * names of its contributors may be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * @category Microsoft
  29. * @package Microsoft_WindowsAzure
  30. * @subpackage UnitTests
  31. * @version $Id: BlobStorageTest.php 14561 2009-05-07 08:05:12Z unknown $
  32. * @copyright Copyright (c) 2009, RealDolmen (http://www.realdolmen.com)
  33. * @license http://phpazure.codeplex.com/license
  34. */
  35. if (!defined('PHPUnit_MAIN_METHOD')) {
  36. define('PHPUnit_MAIN_METHOD', 'Microsoft_WindowsAzure_SessionHandlerTest::main');
  37. }
  38. /**
  39. * Test helpers
  40. */
  41. require_once dirname(__FILE__) . '/../../TestHelper.php';
  42. require_once dirname(__FILE__) . '/../../TestConfiguration.php';
  43. require_once 'PHPUnit/Framework/TestCase.php';
  44. /** Microsoft_WindowsAzure_SessionHandler */
  45. require_once 'Microsoft/WindowsAzure/SessionHandler.php';
  46. /** Microsoft_WindowsAzure_Storage_Table */
  47. require_once 'Microsoft/WindowsAzure/Storage/Table.php';
  48. /**
  49. * @category Microsoft
  50. * @package Microsoft_WindowsAzure
  51. * @subpackage UnitTests
  52. * @version $Id: BlobStorageTest.php 14561 2009-05-07 08:05:12Z unknown $
  53. * @copyright Copyright (c) 2009, RealDolmen (http://www.realdolmen.com)
  54. * @license http://phpazure.codeplex.com/license
  55. */
  56. class Microsoft_WindowsAzure_SessionHandlerTest extends PHPUnit_Framework_TestCase
  57. {
  58. public function __construct()
  59. {
  60. }
  61. public static function main()
  62. {
  63. if (TESTS_SESSIONHANDLER_RUNTESTS) {
  64. $suite = new PHPUnit_Framework_TestSuite("Microsoft_WindowsAzure_SessionHandlerTest");
  65. $result = PHPUnit_TextUI_TestRunner::run($suite);
  66. }
  67. }
  68. /**
  69. * Test setup
  70. */
  71. protected function setUp()
  72. {
  73. }
  74. /**
  75. * Test teardown
  76. */
  77. protected function tearDown()
  78. {
  79. $storageClient = $this->createStorageInstance();
  80. for ($i = 1; $i <= self::$uniqId; $i++)
  81. {
  82. try { $storageClient->deleteTable(TESTS_SESSIONHANDLER_TABLENAME_PREFIX . $i); } catch (Exception $e) { }
  83. }
  84. }
  85. protected function createStorageInstance()
  86. {
  87. $storageClient = null;
  88. if (TESTS_SESSIONHANDLER_RUNONPROD) {
  89. $storageClient = new Microsoft_WindowsAzure_Storage_Table(TESTS_TABLE_HOST_PROD, TESTS_STORAGE_ACCOUNT_PROD, TESTS_STORAGE_KEY_PROD, false, Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract::retryN(10, 250));
  90. } else {
  91. $storageClient = new Microsoft_WindowsAzure_Storage_Table(TESTS_TABLE_HOST_DEV, TESTS_STORAGE_ACCOUNT_DEV, TESTS_STORAGE_KEY_DEV, true, Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract::retryN(10, 250));
  92. }
  93. if (TESTS_STORAGE_USEPROXY) {
  94. $storageClient->setProxy(TESTS_STORAGE_USEPROXY, TESTS_STORAGE_PROXY, TESTS_STORAGE_PROXY_PORT, TESTS_STORAGE_PROXY_CREDENTIALS);
  95. }
  96. return $storageClient;
  97. }
  98. protected function createSessionHandler($storageInstance, $tableName)
  99. {
  100. $sessionHandler = new Microsoft_WindowsAzure_SessionHandler(
  101. $storageInstance,
  102. $tableName
  103. );
  104. return $sessionHandler;
  105. }
  106. protected static $uniqId = 0;
  107. protected function generateName()
  108. {
  109. self::$uniqId++;
  110. return TESTS_SESSIONHANDLER_TABLENAME_PREFIX . self::$uniqId;
  111. }
  112. /**
  113. * Test register
  114. */
  115. public function testRegister()
  116. {
  117. if (TESTS_SESSIONHANDLER_RUNTESTS) {
  118. $storageClient = $this->createStorageInstance();
  119. $tableName = $this->generateName();
  120. $sessionHandler = $this->createSessionHandler($storageClient, $tableName);
  121. $result = $sessionHandler->register();
  122. $this->assertTrue($result);
  123. }
  124. }
  125. /**
  126. * Test open
  127. */
  128. public function testOpen()
  129. {
  130. if (TESTS_SESSIONHANDLER_RUNTESTS) {
  131. $storageClient = $this->createStorageInstance();
  132. $tableName = $this->generateName();
  133. $sessionHandler = $this->createSessionHandler($storageClient, $tableName);
  134. $result = $sessionHandler->open();
  135. $this->assertTrue($result);
  136. $verifyResult = $storageClient->listTables();
  137. $this->assertEquals($tableName, $verifyResult[0]->Name);
  138. }
  139. }
  140. /**
  141. * Test close
  142. */
  143. public function testClose()
  144. {
  145. if (TESTS_SESSIONHANDLER_RUNTESTS) {
  146. $storageClient = $this->createStorageInstance();
  147. $tableName = $this->generateName();
  148. $sessionHandler = $this->createSessionHandler($storageClient, $tableName);
  149. $sessionHandler->open();
  150. $result = $sessionHandler->close();
  151. $this->assertTrue($result);
  152. }
  153. }
  154. /**
  155. * Test read
  156. */
  157. public function testRead()
  158. {
  159. if (TESTS_SESSIONHANDLER_RUNTESTS) {
  160. $storageClient = $this->createStorageInstance();
  161. $tableName = $this->generateName();
  162. $sessionHandler = $this->createSessionHandler($storageClient, $tableName);
  163. $sessionHandler->open();
  164. $sessionId = $this->session_id();
  165. $sessionData = serialize( 'PHPAzure' );
  166. $sessionHandler->write($sessionId, $sessionData);
  167. $result = unserialize( $sessionHandler->read($sessionId) );
  168. $this->assertEquals('PHPAzure', $result);
  169. }
  170. }
  171. /**
  172. * Test write
  173. */
  174. public function testWrite()
  175. {
  176. if (TESTS_SESSIONHANDLER_RUNTESTS) {
  177. $storageClient = $this->createStorageInstance();
  178. $tableName = $this->generateName();
  179. $sessionHandler = $this->createSessionHandler($storageClient, $tableName);
  180. $sessionHandler->open();
  181. $sessionId = $this->session_id();
  182. $sessionData = serialize( 'PHPAzure' );
  183. $sessionHandler->write($sessionId, $sessionData);
  184. $verifyResult = $storageClient->retrieveEntities($tableName);
  185. $this->assertEquals(1, count($verifyResult));
  186. }
  187. }
  188. /**
  189. * Test destroy
  190. */
  191. public function testDestroy()
  192. {
  193. if (TESTS_SESSIONHANDLER_RUNTESTS) {
  194. $storageClient = $this->createStorageInstance();
  195. $tableName = $this->generateName();
  196. $sessionHandler = $this->createSessionHandler($storageClient, $tableName);
  197. $sessionHandler->open();
  198. $sessionId = $this->session_id();
  199. $sessionData = serialize( 'PHPAzure' );
  200. $sessionHandler->write($sessionId, $sessionData);
  201. $result = $sessionHandler->destroy($sessionId);
  202. $this->assertTrue($result);
  203. $verifyResult = $storageClient->retrieveEntities($tableName);
  204. $this->assertEquals(0, count($verifyResult));
  205. }
  206. }
  207. /**
  208. * Test gc
  209. */
  210. public function testGc()
  211. {
  212. if (TESTS_SESSIONHANDLER_RUNTESTS) {
  213. $storageClient = $this->createStorageInstance();
  214. $tableName = $this->generateName();
  215. $sessionHandler = $this->createSessionHandler($storageClient, $tableName);
  216. $sessionHandler->open();
  217. $sessionId = $this->session_id();
  218. $sessionData = serialize( 'PHPAzure' );
  219. $sessionHandler->write($sessionId, $sessionData);
  220. sleep(1); // let time() tick
  221. $result = $sessionHandler->gc(0);
  222. $this->assertTrue($result);
  223. $verifyResult = $storageClient->retrieveEntities($tableName);
  224. $this->assertEquals(0, count($verifyResult));
  225. }
  226. }
  227. protected function session_id()
  228. {
  229. return md5(self::$uniqId);
  230. }
  231. }
  232. // Call Microsoft_WindowsAzure_SessionHandlerTest::main() if this source file is executed directly.
  233. if (PHPUnit_MAIN_METHOD == "Microsoft_WindowsAzure_SessionHandlerTest::main") {
  234. Microsoft_WindowsAzure_SessionHandlerTest::main();
  235. }