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

service/dap: avoid hardcoded []byte slices in DAP tests #1881

Merged
merged 16 commits into from
Feb 24, 2020
Merged

service/dap: avoid hardcoded []byte slices in DAP tests #1881

merged 16 commits into from
Feb 24, 2020

Conversation

eliben
Copy link
Contributor

@eliben eliben commented Feb 20, 2020

DAP tests will check server responses by analyzing interesting fields on a field-by-field basis rather than comparing serialized messages with []byte golden strings.

Updates #1515

@eliben
Copy link
Contributor Author

eliben commented Feb 20, 2020

@polinasok

}

func (c *Client) ExpectContinueResponse(t *testing.T) *dap.ContinueResponse {
m, err := dap.ReadProtocolMessage(c.reader)
Copy link
Member

Choose a reason for hiding this comment

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

I think you would be better served by defining a helper function:

func mustReadProtocolMessage(t *testing.T, c *Client) dap.Message {
   m, err := dap.ReadProtocolMessage(c.reader)
   if err != nil {
       t.Error(err)
   }
   return m
}

then you wouldn't even need these, you could just mustReadProtocolMessage(t, c).(*dap.ConcreteResponseType)

Copy link
Contributor Author

@eliben eliben Feb 21, 2020

Choose a reason for hiding this comment

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

Thanks for the suggestion. Just to make sure I understand precisely, so you mean I should use (1) this helper function to simplify the Expect* methods, or (2) replace all calls to Expect* with this?

E.g., replace:

contResp := client.ExpectContinueResponse(t)

With:

contResp := mustReadProtocolMessage(t, client).(*dap.ContinueResponse)

The tradeoff, IMHO is:

  • The original has cleaner call sites in tests, so the tests themselves are somewhat easier to read.
  • We have some custom logic in some Expect* methods (e.g. ExpectInitializeResponse) and may add more such logic in the future. Removing the helpers means this logic needs to be duplicated at call sites, or have specific helpers just for relevant methods.

On the other hand, your proposal helps remove a bunch of repetitive methods, which is an advantage.

A middle way is (1), replace:

func (c *Client) ExpectContinueResponse(t *testing.T) *dap.ContinueResponse {
	m, err := dap.ReadProtocolMessage(c.reader)
	if err != nil {
		t.Error(err)
	}
	return m.(*dap.ContinueResponse)
}

By:

func (c *Client) ExpectContinueResponse(t *testing.T) *dap.ContinueResponse {
       return c.mustReadProtocolMessage().(*dap.ContinueResponse)
}

This keeps all the Expect* methods but makes them one-liners, except those where extra logic is needed. The call sites in tests remain short and readable, but the amount of repetitive code is reduced.

WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

I was thinking just:

contResp := mustReadProtocolMessage(t, client).(*dap.ContinueResponse)

but if you think you'll need to add extra logic then keeping the Expect methods makes sense.

Copy link
Collaborator

@polinasok polinasok Feb 21, 2020

Choose a reason for hiding this comment

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

I like the idea of keeping the Expect methods, but factoring out all the common parts into a mustRead helper.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Except for consistency it should be called ExpectProtocolMessage or the other one should be called MustReadFooRequest.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

// ReadBaseMessage reads and returns a json-encoded DAP message.
func (c *Client) ReadBaseMessage() ([]byte, error) {
message, err := dap.ReadBaseMessage(c.reader)
func (c *Client) ExpectDisconnectResponse(t *testing.T) *dap.DisconnectResponse {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should these be calling t.Helper()?
(Just learned about it from PR #1882)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@eliben
Copy link
Contributor Author

eliben commented Feb 21, 2020

The 1.12 macOS builder errors out with:

Building without native backend

go build "-ldflags=-s -X main.Build=1f13e2e24d039036bccdd2d9c4a679aca0b75dd1" github.com/go-delve/delve/cmd/dlv

Testing

ok  	github.com/go-delve/delve/cmd/dlv	28.310s

?   	github.com/go-delve/delve/cmd/dlv/cmds	[no test files]

ok  	github.com/go-delve/delve/pkg/config	0.049s

?   	github.com/go-delve/delve/pkg/dwarf/dwarfbuilder	[no test files]

ok  	github.com/go-delve/delve/pkg/dwarf/frame	0.048s

?   	github.com/go-delve/delve/pkg/dwarf/godwarf	[no test files]

ok  	github.com/go-delve/delve/pkg/dwarf/line	1.756s

?   	github.com/go-delve/delve/pkg/dwarf/loclist	[no test files]

ok  	github.com/go-delve/delve/pkg/dwarf/op	0.049s

?   	github.com/go-delve/delve/pkg/dwarf/reader	[no test files]

ok  	github.com/go-delve/delve/pkg/dwarf/util	0.049s

ok  	github.com/go-delve/delve/pkg/goversion	0.075s

?   	github.com/go-delve/delve/pkg/logflags	[no test files]

ok  	github.com/go-delve/delve/pkg/proc	118.970s

ok  	github.com/go-delve/delve/pkg/proc/core	0.054s

?   	github.com/go-delve/delve/pkg/proc/core/minidump	[no test files]

ok  	github.com/go-delve/delve/pkg/proc/fbsdutil	0.052s

ok  	github.com/go-delve/delve/pkg/proc/gdbserial	1.676s

ok  	github.com/go-delve/delve/pkg/proc/linutil	0.056s

?   	github.com/go-delve/delve/pkg/proc/native	[no test files]

?   	github.com/go-delve/delve/pkg/proc/test	[no test files]

?   	github.com/go-delve/delve/pkg/proc/winutil	[no test files]

ok  	github.com/go-delve/delve/pkg/terminal	18.903s

?   	github.com/go-delve/delve/pkg/terminal/starbind	[no test files]

?   	github.com/go-delve/delve/pkg/version	[no test files]

?   	github.com/go-delve/delve/service	[no test files]

?   	github.com/go-delve/delve/service/api	[no test files]

ok  	github.com/go-delve/delve/service/dap	1.533s

?   	github.com/go-delve/delve/service/dap/daptest	[no test files]

ok  	github.com/go-delve/delve/service/debugger	0.053s

?   	github.com/go-delve/delve/service/rpc1	[no test files]

?   	github.com/go-delve/delve/service/rpc2	[no test files]

?   	github.com/go-delve/delve/service/rpccommon	[no test files]

No output has been received in the last 10m0s, this potentially indicates a stalled build or something wrong with the build itself.

Check the details on how to adjust your build configuration on: https://docs.travis-ci.com/user/common-build-problems/#Build-times-out-because-no-output-was-received

Is this known flakiness? (timeout?)

Is there a way to kick a build without pushing a new commit?

Copy link
Member

@aarzilli aarzilli left a comment

Choose a reason for hiding this comment

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

LGTM

@aarzilli
Copy link
Member

PS. I think the timeout is a problem with travis, actually.

Copy link
Member

@derekparker derekparker left a comment

Choose a reason for hiding this comment

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

LGTM

@derekparker derekparker merged commit 7f14f6a into go-delve:master Feb 24, 2020
cgxxv pushed a commit to cgxxv/delve that referenced this pull request Mar 25, 2022
* Remove hardcoded tests

* Cleanup - consistent naming, simplify some code, remove unused func

* Address review comments

* Add Seq checks back

* Address more review comments

* Remove hardcoded tests

* Cleanup - consistent naming, simplify some code, remove unused func

* Address review comments

* Add Seq checks back

* Address more review comments

* Make more consistent

* More consistency

* Simplify Except* methods with a helper

Mark t.Helper() where appropriate
abner-chenc pushed a commit to loongson/delve that referenced this pull request Mar 1, 2024
* Remove hardcoded tests

* Cleanup - consistent naming, simplify some code, remove unused func

* Address review comments

* Add Seq checks back

* Address more review comments

* Remove hardcoded tests

* Cleanup - consistent naming, simplify some code, remove unused func

* Address review comments

* Add Seq checks back

* Address more review comments

* Make more consistent

* More consistency

* Simplify Except* methods with a helper

Mark t.Helper() where appropriate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants