Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#117 - fix data races #119

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open

#117 - fix data races #119

wants to merge 22 commits into from

Conversation

mkenney
Copy link
Owner

@mkenney mkenney commented Aug 30, 2018

Change type

What types of changes does your code introduce to go-chrome?

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Other (please explain here)

Fixing data race issues in test mocks and API.

Checklist

  • I have read the CONTRIBUTING guidelines.
  • Lint and unit tests pass locally with my changes.
  • I have created tests which fail without the change (if possible) and prove my fix is effective or that my feature works.
  • I have added necessary documentation (if appropriate).

What does this implement/fix? Please explain these changes.

Fixes a longstanding issue with data races in the library

  • Replaces the websocket mock with a service using net/http/httptest
  • Fixes unaddressed data races in the library
  • Adds -race to the CI test flags

These changes cause the Ci builds to take about 3x longer (~10 minutes for tip builds, ~3 minutes for other builds), I'll review the throttling behavior separately.

Does this close any currently open issues?

#117

Any relevant logs, error output, etc?

Refer to the output of go test -race ./...

Any other comments?

stability-alpha

tot/socket/cdtp.tracing_test.go Outdated Show resolved Hide resolved
tot/socket/cdtp.tracing_test.go Outdated Show resolved Hide resolved
tot/socket/cdtp.tracing_test.go Outdated Show resolved Hide resolved
@@ -19,7 +19,8 @@ func NewMock(socketURL *url.URL) *Socket {
commandIDMux: &sync.Mutex{},
commands: NewCommandMap(),
handlers: NewEventHandlerMap(),
mux: &sync.Mutex{},
connMux: &sync.Mutex{},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need more than muxer

socket.mux.Lock()
defer socket.mux.Unlock()
socket.connMux.Lock()
defer socket.connMux.Unlock()

if socket.connected {
Copy link

@canhlinh canhlinh Aug 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, My idea is

/*
Connected returns whether a connection exists.

Connected is a Conner implementation.
*/
func (socket *Socket) Connected() bool {
	socket.stateMux.Lock()
	defer socket.stateMux.Unlock()
	return socket.connected
}

// SetConnected updates connected status
func (socket *Socket) setConnectedState(isConnected bool) {
	socket.stateMux.Lock()
	defer socket.stateMux.Unlock()
	socket.connected = isConnected
}

// IsListening check whether the socket is listening
func (socket Socket) IsListening() bool {
	socket.stateMux.Lock()
	defer socket.stateMux.Unlock()

	return socket.listening
}

// setListeningState update listening status
func (socket Socket) setListeningState(isListening bool) {
	socket.stateMux.Lock()
	defer socket.stateMux.Unlock()

	socket.listening = isListening
}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that adds unnecessary complexity for a simple piece of code, but I agree the connected property does the same thing. I can use the value of the conn instead and get rid of the connected property entirely.

@canhlinh
Copy link

There are some confusions on the code. What is purpose of Stop, Disconnect functions ? Please clarify which purpose for them, to make the code cleaner

@canhlinh
Copy link

canhlinh commented Aug 30, 2018

A lot of logging the good for debug, But it make the code a bit harder for reading.

@@ -345,6 +351,7 @@ func (socket *Socket) listen() error {
socket.handleUnknown(response)
}

socket.listenMux.Lock()
if !socket.listening {
Copy link

@canhlinh canhlinh Aug 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should break the loop by using channel
Example:

for {
     select {
            case <-quit:
            break
      default:
           // Continue loop
     }
}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I'll make that change.

@mkenney
Copy link
Owner Author

mkenney commented Aug 30, 2018

The Stop function stops the websocket read loop, while the Disconnect function disconnects the websocket. They don't need to be separated the way they but they are helpful when mocking socket connections in unit tests.

The change I'm making to use the httptest package should remove the need to mock the socket connections, I'll know more when I get further through the changes.

tot/socket/cdtp.tracing_test.go Outdated Show resolved Hide resolved
tot/socket/cdtp.tracing_test.go Outdated Show resolved Hide resolved
tot/socket/socket.socketer.go Outdated Show resolved Hide resolved
tot/socket/mock.chrome_test.go Show resolved Hide resolved
tot/socket/cdtp.tracing_test.go Outdated Show resolved Hide resolved
tot/socket/cdtp.tracing_test.go Outdated Show resolved Hide resolved
tot/socket/cdtp.tracing_test.go Outdated Show resolved Hide resolved
tot/socket/cdtp.tracing_test.go Outdated Show resolved Hide resolved
tot/socket/mock.chrome_test.go Outdated Show resolved Hide resolved
tot/socket/mock.chrome_test.go Outdated Show resolved Hide resolved
tot/socket/socket.socketer.go Outdated Show resolved Hide resolved
tot/socket/socket.socketer.go Outdated Show resolved Hide resolved
@mkenney
Copy link
Owner Author

mkenney commented Sep 1, 2018

So these changes are fixing the data races in the tests, but there's still an issue with stoping the process, it's getting hung on a channel with no listener. I'll investigate further when I have a moment.

@mkenney mkenney added the bug label Sep 1, 2018
@canhlinh
Copy link

canhlinh commented Sep 2, 2018

I think the pain point is how you using muxer. It’s might create a lot of deadlocks in the lib

@mkenney
Copy link
Owner Author

mkenney commented Sep 5, 2018

I agree, I think I'm going to review the entire setup.

tot/socket/cdtp.tracing_test.go Show resolved Hide resolved
tot/socket/cdtp.tracing_test.go Show resolved Hide resolved
tot/socket/cdtp.tracing_test.go Show resolved Hide resolved
tot/socket/socket.socketer.go Show resolved Hide resolved
tot/socket/socket.socketer.go Outdated Show resolved Hide resolved
tot/socket/socket.socketer.go Outdated Show resolved Hide resolved
func (socket *MockSocket) Stop() error {
return nil
func (socket *MockSocket) Stop() {
return

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant return statement

}
return socket.listenErr
return

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant return statement

case dataChan <- <-dataFeed:
}
}
return nil

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unreachable code

chrome.logger.Debug("mock data written to mock chrome websocket")
}
}
return nil

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unreachable code

tot/socket/cdtp.animation_test.go Show resolved Hide resolved
tot/socket/mock.chrome_test.go Show resolved Hide resolved
tot/socket/mock.chrome_test.go Show resolved Hide resolved
tot/socket/socket.conner_test.go Show resolved Hide resolved
tot/socket/socket.socketer.go Outdated Show resolved Hide resolved
tot/socket/socket.socketer_test.go Show resolved Hide resolved
tot/socket/socket.socketer_test.go Show resolved Hide resolved
@mkenney mkenney added this to the v1.0.0 milestone Sep 23, 2018
@mkenney mkenney added the help wanted Looking for technical assistance with this issue. label Sep 23, 2018
@mkenney mkenney self-assigned this Sep 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug help wanted Looking for technical assistance with this issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants