Fix races and remove unreachable code
This commit is contained in:
parent
7ba2fb4c0e
commit
2406f3f09a
2 changed files with 16 additions and 4 deletions
|
|
@ -114,7 +114,7 @@ func (s *Session) heartbeat(ctx context.Context, interval time.Duration) {
|
|||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
s.logger.Println("Running session heartbeat")
|
||||
s.logger.Println("Heartbeat tick")
|
||||
reason := <-s.keepAliveReason
|
||||
s.logger.Println("Keep alive reason: " + reason)
|
||||
if err := s.notifyHostOfActivity(ctx, reason); err != nil {
|
||||
|
|
@ -122,7 +122,6 @@ func (s *Session) heartbeat(ctx context.Context, interval time.Duration) {
|
|||
}
|
||||
}
|
||||
}
|
||||
s.logger.Println("Ending session heartbeat")
|
||||
}
|
||||
|
||||
// notifyHostOfActivity notifies the Live Share host of client activity.
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
|
@ -295,9 +296,14 @@ func TestNotifyHostOfActivity(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestSessionHeartbeat(t *testing.T) {
|
||||
var requests int
|
||||
var (
|
||||
requestsMu sync.Mutex
|
||||
requests int
|
||||
)
|
||||
notifyHostOfActivity := func(rpcReq *jsonrpc2.Request) (interface{}, error) {
|
||||
requestsMu.Lock()
|
||||
requests++
|
||||
requestsMu.Unlock()
|
||||
|
||||
var req []interface{}
|
||||
if err := json.Unmarshal(*rpcReq.Params, &req); err != nil {
|
||||
|
|
@ -369,21 +375,28 @@ func TestSessionHeartbeat(t *testing.T) {
|
|||
}
|
||||
|
||||
type mockLogger struct {
|
||||
sync.Mutex
|
||||
buf *bytes.Buffer
|
||||
}
|
||||
|
||||
func newMockLogger() *mockLogger {
|
||||
return &mockLogger{new(bytes.Buffer)}
|
||||
return &mockLogger{buf: new(bytes.Buffer)}
|
||||
}
|
||||
|
||||
func (m *mockLogger) Printf(format string, v ...interface{}) (int, error) {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
return m.buf.WriteString(fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
func (m *mockLogger) Println(v ...interface{}) (int, error) {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
return m.buf.WriteString(fmt.Sprintln(v...))
|
||||
}
|
||||
|
||||
func (m *mockLogger) String() string {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
return m.buf.String()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue