forwardchannelrequestpool_test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2013 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Unit tests for
  16. * goog.labs.net.webChannel.ForwardChannelRequestPool.
  17. * @suppress {accessControls} Private methods are accessed for test purposes.
  18. *
  19. */
  20. goog.provide('goog.labs.net.webChannel.forwardChannelRequestPoolTest');
  21. goog.require('goog.labs.net.webChannel.ChannelRequest');
  22. goog.require('goog.labs.net.webChannel.ForwardChannelRequestPool');
  23. goog.require('goog.testing.PropertyReplacer');
  24. goog.require('goog.testing.asserts');
  25. goog.require('goog.testing.jsunit');
  26. goog.setTestOnly('goog.labs.net.webChannel.forwardChannelRequestPoolTest');
  27. var propertyReplacer = new goog.testing.PropertyReplacer();
  28. var req = new goog.labs.net.webChannel.ChannelRequest(null, null);
  29. function shouldRunTests() {
  30. return goog.labs.net.webChannel.ChannelRequest.supportsXhrStreaming();
  31. }
  32. function setUp() {}
  33. function tearDown() {
  34. propertyReplacer.reset();
  35. }
  36. function stubSpdyCheck(spdyEnabled) {
  37. propertyReplacer.set(
  38. goog.labs.net.webChannel.ForwardChannelRequestPool, 'isSpdyEnabled_',
  39. function() { return spdyEnabled; });
  40. }
  41. function testSpdyEnabled() {
  42. stubSpdyCheck(true);
  43. var pool = new goog.labs.net.webChannel.ForwardChannelRequestPool();
  44. assertFalse(pool.isFull());
  45. assertEquals(0, pool.getRequestCount());
  46. pool.addRequest(req);
  47. assertTrue(pool.hasPendingRequest());
  48. assertTrue(pool.hasRequest(req));
  49. pool.removeRequest(req);
  50. assertFalse(pool.hasPendingRequest());
  51. for (var i = 0; i < pool.getMaxSize(); i++) {
  52. pool.addRequest(new goog.labs.net.webChannel.ChannelRequest(null, null));
  53. }
  54. assertTrue(pool.isFull());
  55. // do not fail
  56. pool.addRequest(req);
  57. assertTrue(pool.isFull());
  58. }
  59. function testSpdyNotEnabled() {
  60. stubSpdyCheck(false);
  61. var pool = new goog.labs.net.webChannel.ForwardChannelRequestPool();
  62. assertFalse(pool.isFull());
  63. assertEquals(0, pool.getRequestCount());
  64. pool.addRequest(req);
  65. assertTrue(pool.hasPendingRequest());
  66. assertTrue(pool.hasRequest(req));
  67. assertTrue(pool.isFull());
  68. pool.removeRequest(req);
  69. assertFalse(pool.hasPendingRequest());
  70. // do not fail
  71. pool.addRequest(req);
  72. assertTrue(pool.isFull());
  73. }
  74. function testApplyClientProtocol() {
  75. stubSpdyCheck(false);
  76. var pool = new goog.labs.net.webChannel.ForwardChannelRequestPool();
  77. assertEquals(1, pool.getMaxSize());
  78. pool.applyClientProtocol('spdy/3');
  79. assertTrue(pool.getMaxSize() > 1);
  80. pool.applyClientProtocol('foo-bar'); // no effect
  81. assertTrue(pool.getMaxSize() > 1);
  82. pool = new goog.labs.net.webChannel.ForwardChannelRequestPool();
  83. assertEquals(1, pool.getMaxSize());
  84. pool.applyClientProtocol('quic/x');
  85. assertTrue(pool.getMaxSize() > 1);
  86. pool = new goog.labs.net.webChannel.ForwardChannelRequestPool();
  87. assertEquals(1, pool.getMaxSize());
  88. pool.applyClientProtocol('h2');
  89. assertTrue(pool.getMaxSize() > 1);
  90. stubSpdyCheck(true);
  91. pool = new goog.labs.net.webChannel.ForwardChannelRequestPool();
  92. assertTrue(pool.getMaxSize() > 1);
  93. pool.applyClientProtocol('foo/3'); // no effect
  94. assertTrue(pool.getMaxSize() > 1);
  95. }