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

[common] Add unit test for convertions #1395

Merged
merged 7 commits into from
Nov 8, 2024

Conversation

3vilhamster
Copy link
Contributor

@3vilhamster 3vilhamster commented Nov 7, 2024

What changed?
I've introduced a new helper function PtrOf that could be used instead of helpers. I've left helpers for now since they are used all over the code.
Also, I added tests to conversions and thrift encoders.

Why?
Improving code coverage and logic

How did you test it?
unit tests and code stability

@Groxx

This comment was marked as resolved.

internal/common/thrift_util.go Outdated Show resolved Hide resolved
internal/common/thrift_util.go Outdated Show resolved Hide resolved
internal/common/thrift_util_test.go Outdated Show resolved Hide resolved
Copy link

codecov bot commented Nov 8, 2024

Codecov Report

Attention: Patch coverage is 95.45455% with 1 line in your changes missing coverage. Please review.

Project coverage is 81.80%. Comparing base (2d4612d) to head (a215fbc).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
internal/common/thrift_util.go 88.88% 1 Missing ⚠️
Additional details and impacted files
Files with missing lines Coverage Δ
internal/common/convert.go 100.00% <100.00%> (ø)
internal/common/thrift_util.go 82.14% <88.88%> (ø)

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2d4612d...a215fbc. Read the comment docs.

@3vilhamster 3vilhamster merged commit 7a3beaa into cadence-workflow:master Nov 8, 2024
13 checks passed
@3vilhamster 3vilhamster deleted the common-tests branch November 8, 2024 12:29
// TDeserialize is used to deserialize []byte to thrift TStruct
func TDeserialize(ctx context.Context, t thrift.TStruct, b []byte) (err error) {
return thrift.NewTDeserializer().Read(ctx, t, b)
return t.Transport.Bytes(), t.Protocol.Flush(ctx)
Copy link
Member

Choose a reason for hiding this comment

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

this (and not resetting) is a rather significant set of changes - is there something that implies it's correct? e.g. newer thrift/tchannel docs?

Copy link
Contributor Author

@3vilhamster 3vilhamster Nov 8, 2024

Choose a reason for hiding this comment

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

I've looked into thrift.NewTSerializer() and it uses TMemoryBuffer that has a comment of Flush is Noop for it, since it uses plain buffer.
Also, Protocol.Flush is just a wrapper around Transport.Flush().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

// Flushing a memory buffer is a no-op
func (p *TMemoryBuffer) Flush(ctx context.Context) error {
	return nil
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We always initialize one buffer with

t := thrift.NewTSerializer()

Why it should be reset?

Copy link
Member

@Groxx Groxx Nov 8, 2024

Choose a reason for hiding this comment

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

Why would it be reset: I agree it doesn't look correct, but the only thing that matters is if it is correct. If that hasn't been verified, it needs to be, but hasn't been mentioned anywhere so I'm not sure if it has been. By default we kinda have to assume the current code exists for some reason, unless it's clearly wrong.

Copy link
Member

@Groxx Groxx Nov 8, 2024

Choose a reason for hiding this comment

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

From reading the source code: yea looks fine.

I do think this deserves a comment though, as under normal circumstances this reads like:

bytes := t.Transport.Bytes()
err := t.Transport.Flush(ctx)
return bytes, err

which means to get the bytes before flushing, which is generally nonsense. It's only valid for the specific in-memory implementation that's being used right now, not for the interface in general.
If we have a way to make sure that's always correct, that'd be worth doing, otherwise tbh I think I prefer the explicit branching :\ it'll catch errors on upgrades that might change this, where right now it'll probably just silently drop data.

for i := 0; i < len(objs); i++ {
if !IsThriftType(objs[i]) {
// NOTE: our criteria to use which encoder is simple if all the types are serializable using thrift then we use
// thrift encoder. For everything else we default to gob.
Copy link
Member

@Groxx Groxx Nov 8, 2024

Choose a reason for hiding this comment

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

gob json (we don't use gob anywhere), but this is just a move so it's probably fine. we'll fix it eventually.

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.

3 participants