Skip to content

Commit

Permalink
Adds a new API setIsBuffered to allow clients to set connection statu…
Browse files Browse the repository at this point in the history
…s. This can be used by clients that perform additional tests to determine if a connection is actually buffered or not after initial load.

RELNOTES: Adds a new API setIsBuffered to allow clients to set connection status

PiperOrigin-RevId: 518289912
Change-Id: Iab16a025ef72c545f572fe4aa4e5689011a9447d
  • Loading branch information
Closure Team authored and Copybara-Service committed Mar 21, 2023
1 parent f1959de commit 96278c1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
27 changes: 26 additions & 1 deletion closure/goog/net/browserchannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,24 @@ goog.net.BrowserChannel.prototype.isBuffered = function() {
};


/**
* Sets whether the channel is buffered or not. This state is usually updated in
* goog.net.BrowserChannel.testConnectionFinished, but can be set manually here.
* This updated status will be reflected in subsequent connections and requests
* to the channel.
* NOTE: This should ONLY be used by clients that are certain of their
* connection status, i.e. that have performed additional test channels. Setting
* the wrong buffered status on a client can result in undeliverable responses
* from the server.
* @param {boolean} isBuffered Whether the channel is buffered.
*/
goog.net.BrowserChannel.prototype.setIsBuffered = function(isBuffered) {
'use strict';
this.useChunked_ = !isBuffered;
this.secondTestResults_ = isBuffered;
};


/**
* Returns whether chunked mode is allowed. In certain debugging situations,
* it's useful for the application to have a way to disable chunked mode for a
Expand Down Expand Up @@ -1652,7 +1670,14 @@ goog.net.BrowserChannel.prototype.testConnectionFinished = function(
'use strict';
this.channelDebug_.debug('Test Connection Finished');

this.useChunked_ = this.allowChunkedMode_ && useChunked;
// Use the results of the second phase of the test channel if user decided to
// override the test results while the test was ongoing.
if (this.secondTestResults_ == null) {
this.useChunked_ = this.allowChunkedMode_ && useChunked;
} else {
this.useChunked_ = !this.secondTestResults_;
}

this.lastStatusCode_ = testChannel.getLastStatusCode();
// When using asynchronous test, the channel is already open by connect().
if (!this.asyncTest_) {
Expand Down
24 changes: 24 additions & 0 deletions closure/goog/net/browserchannel_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,9 @@ testSuite({

numStatEvents = 0;
lastStatEvent = null;

/** @suppress {visibility} suppression added to enable type checking */
browserChannel.asyncTest_ = false;
},

tearDown() {
Expand Down Expand Up @@ -1317,4 +1320,25 @@ testSuite({
'0',
browserChannel.backChannelRequest_.requestUri_.queryData_.get('CI'));
},

/** @suppress {visibility} suppression added to enable type checking */
testSetIsBuffered() {
connect();
browserChannel.setIsBuffered(true);
completeBackChannel();
browserChannel.setIsBuffered(false);

// Back channel will still be buffered, because #setIsBuffered was called
// after the back channel was already open.
assertEquals(
'1',
browserChannel.backChannelRequest_.requestUri_.queryData_.get('CI'));

// Subsequent back channels that are opened will be streaming.
completeBackChannel();
assertFalse(browserChannel.isBuffered());
assertEquals(
'0',
browserChannel.backChannelRequest_.requestUri_.queryData_.get('CI'));
},
});

0 comments on commit 96278c1

Please sign in to comment.