github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/tcp/stream-net_test.go (about)

     1  /* For license and copyright information please see the LEGAL file in the code repository */
     2  
     3  package tcp
     4  
     5  import (
     6  	"testing"
     7  )
     8  
     9  func TestSocket_Read(t *testing.T) {
    10  	type args struct {
    11  		b []byte
    12  	}
    13  	tests := []struct {
    14  		name    string
    15  		s       *Socket
    16  		args    args
    17  		wantN   int
    18  		wantErr bool
    19  	}{
    20  		{
    21  			name: "buffer full and return",
    22  			s: &Socket{
    23  				recv: recv{
    24  					// buf: buffer.Queue{},
    25  				},
    26  			},
    27  			args: args{
    28  				b: make([]byte, 120),
    29  			},
    30  			wantN:   120,
    31  			wantErr: false,
    32  		},
    33  		{
    34  			name: "buffer not full and timer based",
    35  			s: &Socket{
    36  				recv: recv{
    37  					// readTimer: timer.Timer{},
    38  					// buf: buffer.Queue{},
    39  				},
    40  			},
    41  			args: args{
    42  				b: make([]byte, 120),
    43  			},
    44  			wantN:   120,
    45  			wantErr: false,
    46  		},
    47  		{
    48  			name: "buffer not full and signal based",
    49  			s: &Socket{
    50  				recv: recv{
    51  					// readTimer: timer.Timer{},
    52  					// buf: buffer.Queue{},
    53  				},
    54  			},
    55  			args: args{
    56  				b: make([]byte, 120),
    57  			},
    58  			wantN:   120,
    59  			wantErr: false,
    60  		},
    61  	}
    62  
    63  	tests[0].s.recv.buf.Init(60)
    64  	tests[0].s.recv.buf.Write(make([]byte, 60))
    65  
    66  	tests[1].s.recv.buf.Init(1024)
    67  	tests[1].s.recv.readTimer.Init(nil)
    68  	tests[1].s.recv.readTimer.Start(10000)
    69  	tests[1].s.recv.buf.Write(make([]byte, 160))
    70  
    71  	tests[2].s.recv.buf.Init(1024)
    72  	tests[2].s.recv.buf.Write(make([]byte, 200))
    73  	tests[2].s.recv.flag <- Flag_PSH
    74  
    75  	for _, tt := range tests {
    76  		t.Run(tt.name, func(t *testing.T) {
    77  			gotN, err := tt.s.Read(tt.args.b)
    78  			if (err != nil) != tt.wantErr {
    79  				t.Errorf("Socket.Read() error = %v, wantErr %v", err, tt.wantErr)
    80  				return
    81  			}
    82  			if gotN != tt.wantN {
    83  				t.Errorf("Socket.Read() = %v, want %v", gotN, tt.wantN)
    84  			}
    85  		})
    86  	}
    87  }