func TestWriteMessagesSkipSend(t *testing.T) {
	numMessagesWritten := 0
	timeFunc := func() int64 { return 0 }
	conn := mockConn{
		CloseFunc: func() error {
			return nil
		},
		WriteMessageFunc: func(m message.Message) error {
			numMessagesWritten++
			return nil
		},
		SetWriteDeadlineFunc: func(t time.Time) error {
			if numMessagesWritten == 0 {
				return nil
			}
			return errors.New("connection cannot set write deadline")
		},
		WriteCloseFunc: func(reason string) error {
			return nil
		},
		WritePingFunc: func() error {
			return nil
		},
	}
	s := Socket{
		log:  logtest.DiscardLogger,
		Conn: &conn,
		Config: Config{
			TimeFunc: timeFunc,
		},
	}
	ctx := context.Background()
	ctx, cancelFunc := context.WithCancel(ctx)
	var wg sync.WaitGroup
	in := make(chan message.Message, 1)
	pingTicker := &time.Ticker{
		C: make(chan time.Time),
	}
	httpPingTicker := &time.Ticker{
		C: make(chan time.Time),
	}
	wg.Add(1)
	go s.writeMessagesSync(ctx, &wg, in, pingTicker, httpPingTicker)
	var m message.Message
	for i := 0; i < 3; i++ {
		in <- m
	}
	cancelFunc()
	wg.Wait()
	if numMessagesWritten != 1 {
		t.Errorf("wanted only 1 message written to the bad connection, got %v", numMessagesWritten)
	}
}
// TestWriteMessagesSkipSend ensures that no messages are sent on the connection after it first fails.
// This prevents deadlocks if the lobby keeps sending it messages before it is closed.
TestWriteMessagesSkipSend ensures that no messages are sent on the connection after it first fails.
This prevents deadlocks if the lobby keeps sending it messages before it is closed.
// TestWriteMessagesSkipSend ensures that no messages are sent on the connection after it first fails . // This prevents deadlocks if the lobby keeps sending it messages before it is closed .
func TestWriteMessagesSkipSend(t *testing.T) {
	numMessagesWritten := 0
	timeFunc := func() int64 { return 0 }
	conn := mockConn{
		CloseFunc: func() error {
			return nil
		},
		WriteMessageFunc: func(m message.Message) error {
			numMessagesWritten++
			return nil
		},
		SetWriteDeadlineFunc: func(t time.Time) error {
			if numMessagesWritten == 0 {
				return nil
			}
			return errors.New("connection cannot set write deadline")
		},
		WriteCloseFunc: func(reason string) error {
			return nil
		},
		WritePingFunc: func() error {
			return nil
		},
	}
	s := Socket{
		log:  logtest.DiscardLogger,
		Conn: &conn,
		Config: Config{
			TimeFunc: timeFunc,
		},
	}
	ctx := context.Background()
	ctx, cancelFunc := context.WithCancel(ctx)
	var wg sync.WaitGroup
	in := make(chan message.Message, 1)
	pingTicker := &time.Ticker{
		C: make(chan time.Time),
	}
	httpPingTicker := &time.Ticker{
		C: make(chan time.Time),
	}
	wg.Add(1)
	go s.writeMessagesSync(ctx, &wg, in, pingTicker, httpPingTicker)
	var m message.Message
	for i := 0; i < 3; i++ {
		in <- m
	}
	cancelFunc()
	wg.Wait()
	if numMessagesWritten != 1 {
		t.Errorf("wanted only 1 message written to the bad connection, got %v", numMessagesWritten)
	}
}
func TestWriteMessagesSkipSend ( t * testing . T ) { numMessagesWritten := 0 timeFunc := func ( ) int64 { return 0 } conn := mockConn { CloseFunc : func ( ) error { return nil } , WriteMessageFunc : func ( m message . Message ) error { numMessagesWritten ++ return nil } , SetWriteDeadlineFunc : func ( t time . Time ) error { if numMessagesWritten == 0 { return nil } return errors . New ( " " ) } , WriteCloseFunc : func ( reason string ) error { return nil } , WritePingFunc : func ( ) error { return nil } , } s := Socket { log : logtest . DiscardLogger , Conn : & conn , Config : Config { TimeFunc : timeFunc , } , } ctx := context . Background ( ) ctx , cancelFunc := context . WithCancel ( ctx ) var wg sync . WaitGroup in := make ( chan message . Message , 1 ) pingTicker := & time . Ticker { C : make ( chan time . Time ) , } httpPingTicker := & time . Ticker { C : make ( chan time . Time ) , } wg . Add ( 1 ) go s . writeMessagesSync ( ctx , & wg , in , pingTicker , httpPingTicker ) var m message . Message for i := 0 ; i < 3 ; i ++ { in <- m } cancelFunc ( ) wg . Wait ( ) if numMessagesWritten != 1 { t . Errorf ( " " , numMessagesWritten ) } }
TestWriteMessagesSkipSend ensures that no messages are sent on the connection after it first fails.
TestWriteMessagesSkipSend ensures that no messages are sent on the connection after it first fails .