ThrottlerPluginTest.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. require_once 'Swift/Plugins/ThrottlerPlugin.php';
  4. require_once 'Swift/Events/SendEvent.php';
  5. require_once 'Swift/Plugins/Sleeper.php';
  6. require_once 'Swift/Plugins/Timer.php';
  7. require_once 'Swift/Mime/Message.php';
  8. class Swift_Plugins_ThrottlerPluginTest extends Swift_Tests_SwiftUnitTestCase
  9. {
  10. public function testBytesPerMinuteThrottling()
  11. {
  12. $sleeper = $this->_createSleeper();
  13. $timer = $this->_createTimer();
  14. //10MB/min
  15. $plugin = new Swift_Plugins_ThrottlerPlugin(
  16. 10000000, Swift_Plugins_ThrottlerPlugin::BYTES_PER_MINUTE,
  17. $sleeper, $timer
  18. );
  19. $this->_checking(Expectations::create()
  20. -> one($timer)->getTimestamp() -> returns(0)
  21. -> one($timer)->getTimestamp() -> returns(1) //expected 0.6
  22. -> one($timer)->getTimestamp() -> returns(1) //expected 1.2 (sleep 1)
  23. -> one($timer)->getTimestamp() -> returns(2) //expected 1.8
  24. -> one($timer)->getTimestamp() -> returns(2) //expected 2.4 (sleep 1)
  25. -> ignoring($timer)
  26. -> exactly(2)->of($sleeper)->sleep(1)
  27. );
  28. //10,000,000 bytes per minute
  29. //100,000 bytes per email
  30. // .: (10,000,000/100,000)/60 emails per second = 1.667 emais/sec
  31. $message = $this->_createMessageWithByteCount(100000); //100KB
  32. $evt = $this->_createSendEvent($message);
  33. for ($i = 0; $i < 5; ++$i)
  34. {
  35. $plugin->beforeSendPerformed($evt);
  36. $plugin->sendPerformed($evt);
  37. }
  38. }
  39. public function testMessagesPerMinuteThrottling()
  40. {
  41. $sleeper = $this->_createSleeper();
  42. $timer = $this->_createTimer();
  43. //60/min
  44. $plugin = new Swift_Plugins_ThrottlerPlugin(
  45. 60, Swift_Plugins_ThrottlerPlugin::MESSAGES_PER_MINUTE,
  46. $sleeper, $timer
  47. );
  48. $this->_checking(Expectations::create()
  49. -> one($timer)->getTimestamp() -> returns(0)
  50. -> one($timer)->getTimestamp() -> returns(0) //expected 1 (sleep 1)
  51. -> one($timer)->getTimestamp() -> returns(2) //expected 2
  52. -> one($timer)->getTimestamp() -> returns(2) //expected 3 (sleep 1)
  53. -> one($timer)->getTimestamp() -> returns(4) //expected 4
  54. -> ignoring($timer)
  55. -> exactly(2)->of($sleeper)->sleep(1)
  56. );
  57. //60 messages per minute
  58. //1 message per second
  59. $message = $this->_createMessageWithByteCount(10);
  60. $evt = $this->_createSendEvent($message);
  61. for ($i = 0; $i < 5; ++$i)
  62. {
  63. $plugin->beforeSendPerformed($evt);
  64. $plugin->sendPerformed($evt);
  65. }
  66. }
  67. // -- Creation Methods
  68. private function _createSleeper()
  69. {
  70. return $this->_mock('Swift_Plugins_Sleeper');
  71. }
  72. private function _createTimer()
  73. {
  74. return $this->_mock('Swift_Plugins_Timer');
  75. }
  76. private function _createMessageWithByteCount($bytes)
  77. {
  78. $this->_bytes = $bytes;
  79. $msg = $this->_mock('Swift_Mime_Message');
  80. $this->_checking(Expectations::create()
  81. -> ignoring($msg)->toByteStream(any()) -> calls(array($this, '_write'))
  82. );
  83. return $msg;
  84. }
  85. private function _createSendEvent($message)
  86. {
  87. $evt = $this->_mock('Swift_Events_SendEvent');
  88. $this->_checking(Expectations::create()
  89. -> ignoring($evt)->getMessage() -> returns($message)
  90. );
  91. return $evt;
  92. }
  93. private $_bytes = 0;
  94. public function _write($invocation)
  95. {
  96. $args = $invocation->getArguments();
  97. $is = $args[0];
  98. for ($i = 0; $i < $this->_bytes; ++$i)
  99. {
  100. $is->write('x');
  101. }
  102. }
  103. }