func TestRunRunnerHandleSocketMessage(t *testing.T) {
	ctx := context.Background()
	ctx, cancelFunc := context.WithCancel(ctx)
	var wg sync.WaitGroup
	in := make(chan message.Message)
	inSM := make(chan message.Socket)
	result := make(chan error)
	pn := player.Name("selene")
	addSocketMr := message.Socket{
		Type:       message.SocketAdd,
		PlayerName: pn,
		Result:     result,
	}
	log := new(logtest.Logger)
	wantSocketMessage := message.Message{
		Type:       message.CreateGame,
		Game:       new(game.Info),
		PlayerName: pn,
		Addr:       "mock conn",
	}
	j := 0
	var readWait sync.WaitGroup
	conn := &mockConn{
		RemoteAddrFunc: func() net.Addr {
			return mockAddr(wantSocketMessage.Addr)
		},
		SetReadDeadlineFunc: func(t time.Time) error {
			return nil
		},
		SetPongHandlerFunc: func(h func(appData string) error) {
			// NOOP
		},
		ReadMessageFunc: func(m *message.Message) error {
			if j > 0 {
				return fmt.Errorf("mock conn only sends one message")
			}
			readWait.Wait()
			*m = wantSocketMessage
			j++
			return nil
		},
		IsNormalCloseFunc: func(err error) bool {
			return true
		},
		WriteCloseFunc: func(reason string) error {
			return nil
		},
		CloseFunc: func() error {
			return nil
		},
	}
	upgradeFunc := func(w http.ResponseWriter, r *http.Request) (Conn, error) {
		return conn, nil
	}
	cfg := RunnerConfig{
		MaxSockets:       1,
		MaxPlayerSockets: 1,
		SocketConfig: Config{
			TimeFunc:       func() int64 { return 0 },
			ReadWait:       2 * time.Hour,
			WriteWait:      1 * time.Hour,
			PingPeriod:     2 * time.Hour,
			HTTPPingPeriod: 3 * time.Hour,
		},
	}
	r := Runner{
		upgradeFunc:   upgradeFunc,
		RunnerConfig:  cfg,
		playerSockets: make(map[player.Name]map[message.Addr]chan<- message.Message, 1),
		log:           log,
	}
	readWait.Add(1)
	out := r.Run(ctx, &wg, in, inSM)
	inSM <- addSocketMr
	addSocketErr := <-result
	got1 := <-out
	readWait.Done()
	got2 := <-out
	cancelFunc()
	wg.Wait()
	switch {
	case addSocketErr != nil:
		t.Errorf("wanted no error adding socket, got: %v", addSocketErr)
	case got1.Type != message.GameInfos:
		t.Errorf("wanted first message to lobby to be for game infos, got: %v", got1)
	case !reflect.DeepEqual(wantSocketMessage, got2):
		t.Errorf("messages from socket not equal:\nwanted: %v\ngot:    %v", wantSocketMessage, got2)
	case log.Empty():
		t.Errorf("wanted error to be logged when socket sent socket runner invalid message")
	}
}
// TestRunRunnerHandleSocketMessage ensures a basic, yet invalid socket messages passes through the runner correctly.
TestRunRunnerHandleSocketMessage ensures a basic, yet invalid socket messages passes through the runner correctly.
// TestRunRunnerHandleSocketMessage ensures a basic yet invalid socket messages passes through the runner correctly .
func TestRunRunnerHandleSocketMessage(t *testing.T) {
	ctx := context.Background()
	ctx, cancelFunc := context.WithCancel(ctx)
	var wg sync.WaitGroup
	in := make(chan message.Message)
	inSM := make(chan message.Socket)
	result := make(chan error)
	pn := player.Name("selene")
	addSocketMr := message.Socket{
		Type:       message.SocketAdd,
		PlayerName: pn,
		Result:     result,
	}
	log := new(logtest.Logger)
	wantSocketMessage := message.Message{
		Type:       message.CreateGame,
		Game:       new(game.Info),
		PlayerName: pn,
		Addr:       "mock conn",
	}
	j := 0
	var readWait sync.WaitGroup
	conn := &mockConn{
		RemoteAddrFunc: func() net.Addr {
			return mockAddr(wantSocketMessage.Addr)
		},
		SetReadDeadlineFunc: func(t time.Time) error {
			return nil
		},
		SetPongHandlerFunc: func(h func(appData string) error) {
		},
		ReadMessageFunc: func(m *message.Message) error {
			if j > 0 {
				return fmt.Errorf("mock conn only sends one message")
			}
			readWait.Wait()
			*m = wantSocketMessage
			j++
			return nil
		},
		IsNormalCloseFunc: func(err error) bool {
			return true
		},
		WriteCloseFunc: func(reason string) error {
			return nil
		},
		CloseFunc: func() error {
			return nil
		},
	}
	upgradeFunc := func(w http.ResponseWriter, r *http.Request) (Conn, error) {
		return conn, nil
	}
	cfg := RunnerConfig{
		MaxSockets:       1,
		MaxPlayerSockets: 1,
		SocketConfig: Config{
			TimeFunc:       func() int64 { return 0 },
			ReadWait:       2 * time.Hour,
			WriteWait:      1 * time.Hour,
			PingPeriod:     2 * time.Hour,
			HTTPPingPeriod: 3 * time.Hour,
		},
	}
	r := Runner{
		upgradeFunc:   upgradeFunc,
		RunnerConfig:  cfg,
		playerSockets: make(map[player.Name]map[message.Addr]chan<- message.Message, 1),
		log:           log,
	}
	readWait.Add(1)
	out := r.Run(ctx, &wg, in, inSM)
	inSM <- addSocketMr
	addSocketErr := <-result
	got1 := <-out
	readWait.Done()
	got2 := <-out
	cancelFunc()
	wg.Wait()
	switch {
	case addSocketErr != nil:
		t.Errorf("wanted no error adding socket, got: %v", addSocketErr)
	case got1.Type != message.GameInfos:
		t.Errorf("wanted first message to lobby to be for game infos, got: %v", got1)
	case !reflect.DeepEqual(wantSocketMessage, got2):
		t.Errorf("messages from socket not equal:\nwanted: %v\ngot:    %v", wantSocketMessage, got2)
	case log.Empty():
		t.Errorf("wanted error to be logged when socket sent socket runner invalid message")
	}
}
func TestRunRunnerHandleSocketMessage ( t * testing . T ) { ctx := context . Background ( ) ctx , cancelFunc := context . WithCancel ( ctx ) var wg sync . WaitGroup in := make ( chan message . Message ) inSM := make ( chan message . Socket ) result := make ( chan error ) pn := player . Name ( " " ) addSocketMr := message . Socket { Type : message . SocketAdd , PlayerName : pn , Result : result , } log := new ( logtest . Logger ) wantSocketMessage := message . Message { Type : message . CreateGame , Game : new ( game . Info ) , PlayerName : pn , Addr : " " , } j := 0 var readWait sync . WaitGroup conn := & mockConn { RemoteAddrFunc : func ( ) net . Addr { return mockAddr ( wantSocketMessage . Addr ) } , SetReadDeadlineFunc : func ( t time . Time ) error { return nil } , SetPongHandlerFunc : func ( h func ( appData string ) error ) { } , ReadMessageFunc : func ( m * message . Message ) error { if j > 0 { return fmt . Errorf ( " " ) } readWait . Wait ( ) * m = wantSocketMessage j ++ return nil } , IsNormalCloseFunc : func ( err error ) bool { return true } , WriteCloseFunc : func ( reason string ) error { return nil } , CloseFunc : func ( ) error { return nil } , } upgradeFunc := func ( w http . ResponseWriter , r * http . Request ) ( Conn , error ) { return conn , nil } cfg := RunnerConfig { MaxSockets : 1 , MaxPlayerSockets : 1 , SocketConfig : Config { TimeFunc : func ( ) int64 { return 0 } , ReadWait : 2 * time . Hour , WriteWait : 1 * time . Hour , PingPeriod : 2 * time . Hour , HTTPPingPeriod : 3 * time . Hour , } , } r := Runner { upgradeFunc : upgradeFunc , RunnerConfig : cfg , playerSockets : make ( map [ player . Name ] map [ message . Addr ] chan <- message . Message , 1 ) , log : log , } readWait . Add ( 1 ) out := r . Run ( ctx , & wg , in , inSM ) inSM <- addSocketMr addSocketErr := <- result got1 := <- out readWait . Done ( ) got2 := <- out cancelFunc ( ) wg . Wait ( ) switch { case addSocketErr != nil : t . Errorf ( " " , addSocketErr ) case got1 . Type != message . GameInfos : t . Errorf ( " " , got1 ) case ! reflect . DeepEqual ( wantSocketMessage , got2 ) : t . Errorf ( " \n \n " , wantSocketMessage , got2 ) case log . Empty ( ) : t . Errorf ( " " ) } }
TestRunRunnerHandleSocketMessage ensures a basic, yet invalid socket messages passes through the runner correctly.
TestRunRunnerHandleSocketMessage ensures a basic yet invalid socket messages passes through the runner correctly .
// NOOP