github.com/ezoic/ws@v1.0.4-0.20220713205711-5c1d69e074c5/read_test.go (about)

     1  package ws
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"reflect"
     8  	"testing"
     9  )
    10  
    11  func TestReadHeader(t *testing.T) {
    12  	for i, test := range append([]RWTestCase{
    13  		{
    14  			Data: bits("0000 0000 0 1111111 10000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000"),
    15  			//                              _______________________________________________________________________
    16  			//                                                                 |
    17  			//                                                            Length value
    18  			Err: true,
    19  		},
    20  	}, RWTestCases...) {
    21  		t.Run(fmt.Sprintf("#%d", i), func(t *testing.T) {
    22  			r := bytes.NewReader(test.Data)
    23  			h, err := ReadHeader(r)
    24  			if test.Err && err == nil {
    25  				t.Errorf("expected error, got nil")
    26  			}
    27  			if !test.Err && err != nil {
    28  				t.Errorf("unexpected error: %s", err)
    29  			}
    30  			if test.Err {
    31  				return
    32  			}
    33  			if !reflect.DeepEqual(h, test.Header) {
    34  				t.Errorf("ReadHeader()\nread:\n\t%#v\nwant:\n\t%#v", h, test.Header)
    35  			}
    36  		})
    37  	}
    38  }
    39  
    40  func BenchmarkReadHeader(b *testing.B) {
    41  	for i, bench := range RWBenchCases {
    42  		b.Run(fmt.Sprintf("%s#%d", bench.label, i), func(b *testing.B) {
    43  			bts := MustCompileFrame(Frame{Header: bench.header})
    44  			rds := make([]io.Reader, b.N)
    45  			for i := 0; i < b.N; i++ {
    46  				rds[i] = bytes.NewReader(bts)
    47  			}
    48  
    49  			b.ResetTimer()
    50  
    51  			for i := 0; i < b.N; i++ {
    52  				_, err := ReadHeader(rds[i])
    53  				if err != nil {
    54  					b.Fatal(err)
    55  				}
    56  			}
    57  		})
    58  	}
    59  }