You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
4.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2011, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <string>
  28. #include <vector>
  29. #include "talk/base/faketaskrunner.h"
  30. #include "talk/base/gunit.h"
  31. #include "talk/base/sigslot.h"
  32. #include "talk/xmllite/xmlelement.h"
  33. #include "talk/xmpp/constants.h"
  34. #include "talk/xmpp/fakexmppclient.h"
  35. #include "talk/xmpp/pingtask.h"
  36. class PingTaskTest;
  37. class PingXmppClient : public buzz::FakeXmppClient {
  38. public:
  39. PingXmppClient(talk_base::TaskParent* parent, PingTaskTest* tst) :
  40. FakeXmppClient(parent), test(tst) {
  41. }
  42. buzz::XmppReturnStatus SendStanza(const buzz::XmlElement* stanza);
  43. private:
  44. PingTaskTest* test;
  45. };
  46. class PingTaskTest : public testing::Test, public sigslot::has_slots<> {
  47. public:
  48. PingTaskTest() : respond_to_pings(true), timed_out(false) {
  49. }
  50. virtual void SetUp() {
  51. runner = new talk_base::FakeTaskRunner();
  52. xmpp_client = new PingXmppClient(runner, this);
  53. }
  54. virtual void TearDown() {
  55. // delete xmpp_client; Deleted by deleting runner.
  56. delete runner;
  57. }
  58. void ConnectTimeoutSignal(buzz::PingTask* task) {
  59. task->SignalTimeout.connect(this, &PingTaskTest::OnPingTimeout);
  60. }
  61. void OnPingTimeout() {
  62. timed_out = true;
  63. }
  64. talk_base::FakeTaskRunner* runner;
  65. PingXmppClient* xmpp_client;
  66. bool respond_to_pings;
  67. bool timed_out;
  68. };
  69. buzz::XmppReturnStatus PingXmppClient::SendStanza(
  70. const buzz::XmlElement* stanza) {
  71. buzz::XmppReturnStatus result = FakeXmppClient::SendStanza(stanza);
  72. if (test->respond_to_pings && (stanza->FirstNamed(buzz::QN_PING) != NULL)) {
  73. std::string ping_response =
  74. "<iq xmlns=\'jabber:client\' id='0' type='result'/>";
  75. HandleStanza(buzz::XmlElement::ForStr(ping_response));
  76. }
  77. return result;
  78. }
  79. TEST_F(PingTaskTest, TestSuccess) {
  80. uint32 ping_period_millis = 100;
  81. buzz::PingTask* task = new buzz::PingTask(xmpp_client,
  82. talk_base::Thread::Current(),
  83. ping_period_millis, ping_period_millis / 10);
  84. ConnectTimeoutSignal(task);
  85. task->Start();
  86. unsigned int expected_ping_count = 5U;
  87. EXPECT_EQ_WAIT(xmpp_client->sent_stanzas().size(), expected_ping_count,
  88. ping_period_millis * (expected_ping_count + 1));
  89. EXPECT_FALSE(task->IsDone());
  90. EXPECT_FALSE(timed_out);
  91. }
  92. TEST_F(PingTaskTest, TestTimeout) {
  93. respond_to_pings = false;
  94. uint32 ping_timeout_millis = 200;
  95. buzz::PingTask* task = new buzz::PingTask(xmpp_client,
  96. talk_base::Thread::Current(),
  97. ping_timeout_millis * 10, ping_timeout_millis);
  98. ConnectTimeoutSignal(task);
  99. task->Start();
  100. WAIT(false, ping_timeout_millis / 2);
  101. EXPECT_FALSE(timed_out);
  102. EXPECT_TRUE_WAIT(timed_out, ping_timeout_millis * 2);
  103. }