-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split apart deferrable range and deferrable value
- Loading branch information
Showing
10 changed files
with
145 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package deferrable | ||
|
||
import "net/http" | ||
|
||
func Flush(w http.ResponseWriter) error { | ||
return http.NewResponseController(w).Flush() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package deferrable | ||
|
||
import "net/http" | ||
|
||
type Range[T any] struct { | ||
writer http.ResponseWriter | ||
channel chan T | ||
} | ||
|
||
func NewRange[T any](writer http.ResponseWriter, channel chan T) Range[T] { | ||
return Range[T]{ | ||
writer: writer, | ||
channel: channel, | ||
} | ||
} | ||
|
||
func (d Range[T]) Get() chan T { | ||
_ = Flush(d.writer) | ||
flushChannel := make(chan T) | ||
|
||
go func() { | ||
for val := range d.channel { | ||
_ = Flush(d.writer) | ||
flushChannel <- val | ||
} | ||
_ = Flush(d.writer) | ||
close(flushChannel) | ||
}() | ||
|
||
return flushChannel | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package deferrable_test | ||
|
||
import ( | ||
"github.com/initialcapacity/go-streaming/pkg/deferrable" | ||
"github.com/initialcapacity/go-streaming/pkg/deferrable/test" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestRange(t *testing.T) { | ||
writer := deferrable_test.ResponseWriterSpy{} | ||
channel := make(chan string) | ||
defer close(channel) | ||
|
||
go func() { | ||
channel <- "pickles" | ||
channel <- "chicken" | ||
}() | ||
|
||
values := deferrable.NewRange(&writer, channel).Get() | ||
|
||
assert.Equal(t, uint64(1), writer.FlushCalls.Load()) | ||
assert.Equal(t, "pickles", <-values) | ||
assert.Equal(t, "chicken", <-values) | ||
assert.Equal(t, uint64(3), writer.FlushCalls.Load()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package deferrable_test | ||
|
||
import ( | ||
"net/http" | ||
"sync/atomic" | ||
) | ||
|
||
type ResponseWriterSpy struct { | ||
FlushCalls atomic.Uint64 | ||
} | ||
|
||
func (w *ResponseWriterSpy) Header() http.Header { | ||
return map[string][]string{} | ||
} | ||
|
||
func (w *ResponseWriterSpy) Write([]byte) (int, error) { | ||
return 0, nil | ||
} | ||
|
||
func (w *ResponseWriterSpy) WriteHeader(_ int) { | ||
} | ||
|
||
func (w *ResponseWriterSpy) Flush() { | ||
w.FlushCalls.Add(1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package deferrable | ||
|
||
import "net/http" | ||
|
||
type Value[T any] struct { | ||
writer http.ResponseWriter | ||
channel chan T | ||
} | ||
|
||
func NewValue[T any](writer http.ResponseWriter, channel chan T) Value[T] { | ||
return Value[T]{ | ||
writer: writer, | ||
channel: channel, | ||
} | ||
} | ||
|
||
func (d Value[T]) Get() T { | ||
_ = Flush(d.writer) | ||
return <-d.channel | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package deferrable_test | ||
|
||
import ( | ||
"github.com/initialcapacity/go-streaming/pkg/deferrable" | ||
"github.com/initialcapacity/go-streaming/pkg/deferrable/test" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestChannel(t *testing.T) { | ||
writer := deferrable_test.ResponseWriterSpy{} | ||
channel := make(chan string) | ||
defer close(channel) | ||
|
||
go func() { | ||
channel <- "pickles" | ||
}() | ||
|
||
value := deferrable.NewValue(&writer, channel).Get() | ||
|
||
assert.Equal(t, "pickles", value) | ||
assert.Equal(t, uint64(1), writer.FlushCalls.Load()) | ||
} |