Repository: pion/webrtc
Pull request #1424 (2020-09-14 10:39:42)

Fix issue 1211
return io.ErrClosedPipe instead of default error coming from ensureOpened

wrote test for attempting to send when close

#### Description

#### Reference issue
Fixes #...https://github.com/pion/webrtc/issues/1211


codecov[bot]: # [Codecov](https://codecov.io/gh/pion/webrtc/pull/1424?src=pr&el=h1) Report
> Merging [#1424](https://codecov.io/gh/pion/webrtc/pull/1424?src=pr&el=desc) into [master](https://codecov.io/gh/pion/webrtc/commit/66ca9ed130c6b059d26e0fb49bac3cc80748a6fb?el=desc) will **increase** coverage by `0.31%`.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/pion/webrtc/pull/1424/graphs/tree.svg?width=650&height=150&src=pr&token=XOOuQo07I2)](https://codecov.io/gh/pion/webrtc/pull/1424?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master    #1424      +/-   ##
==========================================
+ Coverage   71.88%   72.20%   +0.31%     
==========================================
  Files          53       53              
  Lines        2273     2263      -10     
==========================================
  Hits         1634     1634              
+ Misses        541      531      -10     
  Partials       98       98              
```

| Flag | Coverage Δ | |
|---|---|---|
| #wasm | `72.20% <ø> (+0.31%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/pion/webrtc/pull/1424?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [stats.go](https://codecov.io/gh/pion/webrtc/pull/1424/diff?src=pr&el=tree#diff-c3RhdHMuZ28=) | `0.00% <0.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pion/webrtc/pull/1424?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pion/webrtc/pull/1424?src=pr&el=footer). Last update [66ca9ed...61d417f](https://codecov.io/gh/pion/webrtc/pull/1424?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

tarrencev: @obasajujoshua31 you can just delete `ErrDataChannelNotOpen` in `errors.go` and update the return value in `datachannel.go` line 357 to `err. ErrClosedPipe`

Patch:
diff --git a/README.md b/README.md
index 2af52e99..79ef0375 100644
--- a/README.md
+++ b/README.md
@@ -200,6 +200,7 @@ Check out the **[contributing wiki](https://github.com/pion/webrtc/wiki/Contribu
 * [Alessandro Ros](https://github.com/aler9)
 * [Thomas Miller](https://github.com/tmiv)
 * [yoko(q191201771)](https://github.com/q191201771)
+* [Joshua Obasaju](https://github.com/obasajujoshua31)
 
 ### License
 MIT License - see [LICENSE](LICENSE) for full text
diff --git a/datachannel.go b/datachannel.go
index cc406828..d140ea7d 100644
--- a/datachannel.go
+++ b/datachannel.go
@@ -354,7 +354,7 @@ func (d *DataChannel) ensureOpen() error {
 	d.mu.RLock()
 	defer d.mu.RUnlock()
 	if d.readyState != DataChannelStateOpen {
-		return &rtcerr.InvalidStateError{Err: ErrDataChannelNotOpen}
+		return io.ErrClosedPipe
 	}
 	return nil
 }
diff --git a/go.sum b/go.sum
index a68599c3..2a13fc58 100644
--- a/go.sum
+++ b/go.sum
@@ -119,7 +119,6 @@ github.com/pion/rtcp v1.2.3 h1:2wrhKnqgSz91Q5nzYTO07mQXztYPtxL8a0XOss4rJqA=
 github.com/pion/rtcp v1.2.3/go.mod h1:zGhIv0RPRF0Z1Wiij22pUt5W/c9fevqSzT4jje/oK7I=
 github.com/pion/rtp v1.6.0 h1:4Ssnl/T5W2LzxHj9ssYpGVEQh3YYhQFNVmSWO88MMwk=
 github.com/pion/rtp v1.6.0/go.mod h1:QgfogHsMBVE/RFNno467U/KBqfUywEH+HK+0rtnwsdI=
-github.com/pion/sctp v1.7.9 h1:n+A37cTMU08xL3Oodkz39XjtPReQliKyk01q96mGB5M=
 github.com/pion/sctp v1.7.9/go.mod h1:EhpTUQu1/lcK3xI+eriS6/96fWetHGCvBi9MSsnaBN0=
 github.com/pion/sctp v1.7.10 h1:o3p3/hZB5Cx12RMGyWmItevJtZ6o2cpuxaw6GOS4x+8=
 github.com/pion/sctp v1.7.10/go.mod h1:EhpTUQu1/lcK3xI+eriS6/96fWetHGCvBi9MSsnaBN0=
@@ -174,7 +173,6 @@ github.com/shurcooL/users v0.0.0-20180125191416-49c67e49c537/go.mod h1:QJTqeLYED
 github.com/shurcooL/webdavfs v0.0.0-20170829043945-18c3829fa133/go.mod h1:hKmq5kWdCj2z2KEozexVbfEZIWiTjhE0+UjmZgPqehw=
 github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d/go.mod h1:UdhH50NIW0fCiwBSr0co2m7BnFLdv4fQTgdqdJTHFeE=
 github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e/go.mod h1:HuIsMU8RRBOtsCgI77wP899iHVBQpCmg4ErYMZB+2IA=
-github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
 github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
 github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
@@ -214,7 +212,6 @@ golang.org/x/net v0.0.0-20191126235420-ef20fe5d7933/go.mod h1:z5CRVTTTmAJ677TzLL
 golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
 golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
 golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
-golang.org/x/net v0.0.0-20200707034311-ab3426394381 h1:VXak5I6aEWmAXeQjA+QSZzlgNrpq9mjcfDemuexIKsU=
 golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
 golang.org/x/net v0.0.0-20200822124328-c89045814202 h1:VvcQYSHwXgi7W+TpUR6A9g6Up98WAHf3f/ulnJ62IyA=
 golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=


Test patch:
diff --git a/datachannel_ortc_test.go b/datachannel_ortc_test.go
index dac24908..927871f4 100644
--- a/datachannel_ortc_test.go
+++ b/datachannel_ortc_test.go
@@ -3,6 +3,8 @@
 package webrtc
 
 import (
+	"github.com/stretchr/testify/assert"
+	"io"
 	"testing"
 	"time"
 
@@ -75,6 +77,19 @@ func TestDataChannel_ORTCE2E(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
+
+	// attempt to send when channel is closed
+	err = channelA.Send([]byte("ABC"))
+	assert.Error(t, err)
+	assert.Equal(t, io.ErrClosedPipe, err)
+
+	err = channelA.SendText("test")
+	assert.Error(t, err)
+	assert.Equal(t, io.ErrClosedPipe, err)
+
+	err = channelA.ensureOpen()
+	assert.Error(t, err)
+	assert.Equal(t, io.ErrClosedPipe, err)
 }
 
 type testORTCStack struct {