Repository: knative/pkg
Pull request #1684 (2020-09-04 00:23:00)

Add IsEstablished method to ManagedConnection to expose whether the connection has been established
When using a ManagedConnection for unit test, this function can help us know whether the connection has been established so that we can start sending message. Instead of sending message and checking whether it succeeds or not, checking the return of this function is easier.

knative-prow-robot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative/pkg/pull/1684#" title="Author self-approved">yanweiguo</a>*
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **vagababov**
You can assign the PR to them by writing `/assign @vagababov` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative%2Fpkg).

<details open>
Needs approval from an approver in each of these files:

- **[websocket/OWNERS](https://github.com/knative/pkg/blob/master/websocket/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["vagababov"]} -->
yanweiguo: /assign @vagababov 
knative-prow-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative/pkg/pull/1684#pullrequestreview-482283894" title="LGTM">vagababov</a>*, *<a href="https://github.com/knative/pkg/pull/1684#" title="Author self-approved">yanweiguo</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative%2Fpkg).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[websocket/OWNERS](https://github.com/knative/pkg/blob/master/websocket/OWNERS)~~ [vagababov]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
knative-metrics-robot: The following is the coverage report on the affected files.
Say `/test pull-knative-pkg-go-coverage` to re-run this coverage report

File | Old Coverage | New Coverage | Delta
---- |:------------:|:------------:|:-----:
[websocket/connection.go](https://storage.cloud.google.com/knative-prow/pr-logs/pull/knative_pkg/1684/pull-knative-pkg-go-coverage/1301677335593881603/artifacts/line-cov.html#file0) | 96.0% | 95.1% | -0.9

knative-prow-robot: @yanweiguo: The following test **failed**, say `/retest` to rerun all failed tests:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
pull-knative-pkg-unit-tests | 38bb255a104021f2ceda03385030b23b20d4b714 | [link](https://prow.knative.dev/view/gs/knative-prow/pr-logs/pull/knative_pkg/1684/pull-knative-pkg-unit-tests/1301677335593881602) | `/test pull-knative-pkg-unit-tests`

[Full PR test history](https://gubernator.knative.dev/pr/knative_pkg/1684). [Your PR dashboard](https://gubernator.knative.dev/pr/yanweiguo).

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->
yanweiguo: /test pull-knative-pkg-unit-tests

Patch:
diff --git a/websocket/connection.go b/websocket/connection.go
index 380469847..360280227 100644
--- a/websocket/connection.go
+++ b/websocket/connection.go
@@ -327,3 +327,11 @@ func (c *ManagedConnection) Shutdown() error {
 	c.processingWg.Wait()
 	return err
 }
+
+// IsEstablished returns true the websocket connection has been established.
+func (c *ManagedConnection) IsEstablished() bool {
+	c.connectionLock.RLock()
+	defer c.connectionLock.RUnlock()
+
+	return c.connection != nil
+}


Test patch:
diff --git a/websocket/connection_test.go b/websocket/connection_test.go
index 78ea8e498..69a674fe6 100644
--- a/websocket/connection_test.go
+++ b/websocket/connection_test.go
@@ -418,3 +418,17 @@ func TestDurableConnectionSendsPingsRegularly(t *testing.T) {
 		<-pingReceived
 	}
 }
+
+func TestIsEstablished(t *testing.T) {
+	c := &ManagedConnection{}
+	if got, want := c.IsEstablished(), false; got != want {
+		t.Errorf("IsEstablished() = %v, want = %v", got, want)
+	}
+
+	c.connection = &inspectableConnection{
+		writeMessageCalls: make(chan struct{}, 1),
+	}
+	if got, want := c.IsEstablished(), true; got != want {
+		t.Errorf("IsEstablished() = %v, want = %v", got, want)
+	}
+}