...

Source file src/nhooyr.io/websocket/compress_test.go

Documentation: nhooyr.io/websocket

     1  //go:build !js
     2  // +build !js
     3  
     4  package websocket
     5  
     6  import (
     7  	"bytes"
     8  	"compress/flate"
     9  	"io"
    10  	"strings"
    11  	"testing"
    12  
    13  	"nhooyr.io/websocket/internal/test/assert"
    14  	"nhooyr.io/websocket/internal/test/xrand"
    15  )
    16  
    17  func Test_slidingWindow(t *testing.T) {
    18  	t.Parallel()
    19  
    20  	const testCount = 99
    21  	const maxWindow = 99999
    22  	for i := 0; i < testCount; i++ {
    23  		t.Run("", func(t *testing.T) {
    24  			t.Parallel()
    25  
    26  			input := xrand.String(maxWindow)
    27  			windowLength := xrand.Int(maxWindow)
    28  			var sw slidingWindow
    29  			sw.init(windowLength)
    30  			sw.write([]byte(input))
    31  
    32  			assert.Equal(t, "window length", windowLength, cap(sw.buf))
    33  			if !strings.HasSuffix(input, string(sw.buf)) {
    34  				t.Fatalf("r.buf is not a suffix of input: %q and %q", input, sw.buf)
    35  			}
    36  		})
    37  	}
    38  }
    39  
    40  func BenchmarkFlateWriter(b *testing.B) {
    41  	b.ReportAllocs()
    42  	for i := 0; i < b.N; i++ {
    43  		w, _ := flate.NewWriter(io.Discard, flate.BestSpeed)
    44  		// We have to write a byte to get the writer to allocate to its full extent.
    45  		w.Write([]byte{'a'})
    46  		w.Flush()
    47  	}
    48  }
    49  
    50  func BenchmarkFlateReader(b *testing.B) {
    51  	b.ReportAllocs()
    52  
    53  	var buf bytes.Buffer
    54  	w, _ := flate.NewWriter(&buf, flate.BestSpeed)
    55  	w.Write([]byte{'a'})
    56  	w.Flush()
    57  
    58  	for i := 0; i < b.N; i++ {
    59  		r := flate.NewReader(bytes.NewReader(buf.Bytes()))
    60  		io.ReadAll(r)
    61  	}
    62  }
    63  

View as plain text