Repository: pion/interceptor
Pull request #8 (2020-11-30 07:37:06)

Use context based io
Use context.Context to control Read/Write timeout.


codecov[bot]: # [Codecov](https://codecov.io/gh/pion/interceptor/pull/8?src=pr&el=h1) Report
> Merging [#8](https://codecov.io/gh/pion/interceptor/pull/8?src=pr&el=desc) (f4a3bbf) into [master](https://codecov.io/gh/pion/interceptor/commit/1eef658642661f16f65fcf3664e76d9fb1d2e504?el=desc) (1eef658) will **not change** coverage.
> The diff coverage is `0.00%`.

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

```diff
@@           Coverage Diff           @@
##           master       #8   +/-   ##
=======================================
  Coverage   23.52%   23.52%           
=======================================
  Files           6        6           
  Lines          51       51           
=======================================
  Hits           12       12           
  Misses         36       36           
  Partials        3        3           
```

| Flag | Coverage Δ | |
|---|---|---|
| go | `23.52% <0.00%> (ø)` | |
| wasm | `?` | |

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/interceptor/pull/8?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [interceptor.go](https://codecov.io/gh/pion/interceptor/pull/8/diff?src=pr&el=tree#diff-aW50ZXJjZXB0b3IuZ28=) | `0.00% <0.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pion/interceptor/pull/8?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/interceptor/pull/8?src=pr&el=footer). Last update [1eef658...f4a3bbf](https://codecov.io/gh/pion/interceptor/pull/8?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/README.md b/README.md
index 0ef2bb8..f7a39dd 100644
--- a/README.md
+++ b/README.md
@@ -28,6 +28,7 @@ Check out the **[contributing wiki](https://github.com/pion/webrtc/wiki/Contribu
 
 * [Adam Kiss](https://github.com/masterada) - *Original Author*
 * [Sean DuBois](https://github.com/sean-der) - *Original Author*
+* [Atsushi Watanabe](https://github.com/at-wat) - *Original Author*
 
 ### License
 MIT License - see [LICENSE](LICENSE) for full text
diff --git a/interceptor.go b/interceptor.go
index d168a83..655369f 100644
--- a/interceptor.go
+++ b/interceptor.go
@@ -5,6 +5,7 @@
 package interceptor
 
 import (
+	"context"
 	"io"
 
 	"github.com/pion/rtcp"
@@ -43,60 +44,60 @@ type Interceptor interface {
 // RTPWriter is used by Interceptor.BindLocalStream.
 type RTPWriter interface {
 	// Write a rtp packet
-	Write(p *rtp.Packet, attributes Attributes) (int, error)
+	Write(ctx context.Context, p *rtp.Packet, attributes Attributes) (int, error)
 }
 
 // RTPReader is used by Interceptor.BindRemoteStream.
 type RTPReader interface {
 	// Read a rtp packet
-	Read() (*rtp.Packet, Attributes, error)
+	Read(ctx context.Context) (*rtp.Packet, Attributes, error)
 }
 
 // RTCPWriter is used by Interceptor.BindRTCPWriter.
 type RTCPWriter interface {
 	// Write a batch of rtcp packets
-	Write(pkts []rtcp.Packet, attributes Attributes) (int, error)
+	Write(ctx context.Context, pkts []rtcp.Packet, attributes Attributes) (int, error)
 }
 
 // RTCPReader is used by Interceptor.BindRTCPReader.
 type RTCPReader interface {
 	// Read a batch of rtcp packets
-	Read() ([]rtcp.Packet, Attributes, error)
+	Read(ctx context.Context) ([]rtcp.Packet, Attributes, error)
 }
 
 // Attributes are a generic key/value store used by interceptors
 type Attributes map[interface{}]interface{}
 
 // RTPWriterFunc is an adapter for RTPWrite interface
-type RTPWriterFunc func(p *rtp.Packet, attributes Attributes) (int, error)
+type RTPWriterFunc func(ctx context.Context, p *rtp.Packet, attributes Attributes) (int, error)
 
 // RTPReaderFunc is an adapter for RTPReader interface
-type RTPReaderFunc func() (*rtp.Packet, Attributes, error)
+type RTPReaderFunc func(ctx context.Context) (*rtp.Packet, Attributes, error)
 
 // RTCPWriterFunc is an adapter for RTCPWriter interface
-type RTCPWriterFunc func(pkts []rtcp.Packet, attributes Attributes) (int, error)
+type RTCPWriterFunc func(ctx context.Context, pkts []rtcp.Packet, attributes Attributes) (int, error)
 
 // RTCPReaderFunc is an adapter for RTCPReader interface
-type RTCPReaderFunc func() ([]rtcp.Packet, Attributes, error)
+type RTCPReaderFunc func(ctx context.Context) ([]rtcp.Packet, Attributes, error)
 
 // Write a rtp packet
-func (f RTPWriterFunc) Write(p *rtp.Packet, attributes Attributes) (int, error) {
-	return f(p, attributes)
+func (f RTPWriterFunc) Write(ctx context.Context, p *rtp.Packet, attributes Attributes) (int, error) {
+	return f(ctx, p, attributes)
 }
 
 // Read a rtp packet
-func (f RTPReaderFunc) Read() (*rtp.Packet, Attributes, error) {
-	return f()
+func (f RTPReaderFunc) Read(ctx context.Context) (*rtp.Packet, Attributes, error) {
+	return f(ctx)
 }
 
 // Write a batch of rtcp packets
-func (f RTCPWriterFunc) Write(pkts []rtcp.Packet, attributes Attributes) (int, error) {
-	return f(pkts, attributes)
+func (f RTCPWriterFunc) Write(ctx context.Context, pkts []rtcp.Packet, attributes Attributes) (int, error) {
+	return f(ctx, pkts, attributes)
 }
 
 // Read a batch of rtcp packets
-func (f RTCPReaderFunc) Read() ([]rtcp.Packet, Attributes, error) {
-	return f()
+func (f RTCPReaderFunc) Read(ctx context.Context) ([]rtcp.Packet, Attributes, error) {
+	return f(ctx)
 }
 
 // Get returns the attribute associated with key.


Test patch:
diff --git a/interceptor_test.go b/interceptor_test.go
new file mode 100644
index 0000000..082d564
--- /dev/null
+++ b/interceptor_test.go
@@ -0,0 +1,64 @@
+// +build !js
+
+package interceptor
+
+import (
+	"context"
+	"errors"
+	"testing"
+
+	"github.com/pion/rtcp"
+	"github.com/pion/rtp"
+)
+
+func TestRTPReader_ContextCancel(t *testing.T) {
+	r := RTPReaderFunc(func(ctx context.Context) (*rtp.Packet, Attributes, error) {
+		<-ctx.Done()
+		return nil, nil, ctx.Err()
+	})
+	ctx, cancel := context.WithCancel(context.Background())
+	cancel()
+	_, _, err := r.Read(ctx)
+	if !errors.Is(err, context.Canceled) {
+		t.Errorf("Expected '%v', got '%v'", context.Canceled, err)
+	}
+}
+
+func TestRTCPReader_ContextCancel(t *testing.T) {
+	r := RTCPReaderFunc(func(ctx context.Context) ([]rtcp.Packet, Attributes, error) {
+		<-ctx.Done()
+		return nil, nil, ctx.Err()
+	})
+	ctx, cancel := context.WithCancel(context.Background())
+	cancel()
+	_, _, err := r.Read(ctx)
+	if !errors.Is(err, context.Canceled) {
+		t.Errorf("Expected '%v', got '%v'", context.Canceled, err)
+	}
+}
+
+func TestRTPWriter_ContextCancel(t *testing.T) {
+	r := RTPWriterFunc(func(ctx context.Context, p *rtp.Packet, attr Attributes) (int, error) {
+		<-ctx.Done()
+		return 0, ctx.Err()
+	})
+	ctx, cancel := context.WithCancel(context.Background())
+	cancel()
+	_, err := r.Write(ctx, &rtp.Packet{}, nil)
+	if !errors.Is(err, context.Canceled) {
+		t.Errorf("Expected '%v', got '%v'", context.Canceled, err)
+	}
+}
+
+func TestRTCPWriter_ContextCancel(t *testing.T) {
+	r := RTCPWriterFunc(func(ctx context.Context, p []rtcp.Packet, attr Attributes) (int, error) {
+		<-ctx.Done()
+		return 0, ctx.Err()
+	})
+	ctx, cancel := context.WithCancel(context.Background())
+	cancel()
+	_, err := r.Write(ctx, []rtcp.Packet{}, nil)
+	if !errors.Is(err, context.Canceled) {
+		t.Errorf("Expected '%v', got '%v'", context.Canceled, err)
+	}
+}