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

Test failed on different time zone. #3181

Closed
kvii opened this issue Jan 31, 2024 · 1 comment · Fixed by #3183
Closed

Test failed on different time zone. #3181

kvii opened this issue Jan 31, 2024 · 1 comment · Fixed by #3183
Labels
bug Something isn't working

Comments

@kvii
Copy link
Contributor

kvii commented Jan 31, 2024

What happened:

Test failed. Logs like below:

go test -run ^TestEncodeValues$ github.com/go-kratos/kratos/v2/encoding/form

--- FAIL: TestEncodeValues (0.00s)
    /Users/kvii/workspace/github.com/kvii/kratos/encoding/form/proto_encode_test.go:49: want: a=19...&timestamp=1970-01-01T00%3A00%3A20.000000002Z..., got: a=19...&timestamp=1970-01-01T08%3A00%3A20.000000002Z...
FAIL
FAIL	github.com/go-kratos/kratos/v2/encoding/form	1.340s
FAIL

Note that timestamps "1970-01-01T00" and "1970-01-01T08" are different.

What you expected to happen:

Test passed.

How to reproduce it (as minimally and precisely as possible):

go test -run ^TestEncodeValues$ github.com/go-kratos/kratos/v2/encoding/form.

Anything else we need to know?:

I'll fix it soon.

Environment:

  • Kratos version (use kratos -v): v2.7.2
  • Go version (use go version): go1.21.6 darwin/arm64
  • OS (e.g: cat /etc/os-release): Mac OS Sonoma 14.3
  • Others:
@kvii kvii added the bug Something isn't working label Jan 31, 2024
Copy link

dosubot bot commented Jan 31, 2024

🤖

Hey @kvii, good to see you diving into the time zone mystery! Hope all's well on your end.

The TestEncodeValues test could be failing due to a difference in timestamps because of the way the timestamp is being encoded and compared. In the test, the timestamp is being encoded into a string format and then compared with a hardcoded string value.

This could lead to a failure in the test if the timestamp is not being encoded into the exact same string format as the hardcoded string value.

In the test, the timestamp is being encoded as follows:

Timestamp: &timestamppb.Timestamp{Seconds: 20, Nanos: 2},

And it is being compared with the following hardcoded string value:

"timestamp=1970-01-01T00%3A00%3A20.000000002Z"

If the encoding of the timestamp does not result in the exact same string format, the test will fail.

One possible solution could be to adjust the way the timestamp is being encoded or to adjust the hardcoded string value to match the format of the encoded timestamp.

Another possible solution could be to compare the timestamps in a different way that does not rely on the string format. For example, you could compare the seconds and nanoseconds of the timestamps directly instead of comparing the string formats.

However, without more information about the specific error message or the exact difference in the timestamps, it's hard to provide a more specific solution.

The TestEncodeValues function in the Kratos codebase is a unit test function that tests the EncodeValues function. The EncodeValues function is used to encode the values of a complex structure into a URL-encoded string. This is useful when you want to send complex data structures as part of a URL query string.

In the TestEncodeValues function, a complex structure is created and passed to the EncodeValues function. The returned query string is then compared with the expected string. If the returned string does not match the expected string, the test fails, indicating that the EncodeValues function is not working as expected.

Here is the relevant code:

func TestEncodeValues(t *testing.T) {
	// ... complex structure creation ...
	query, err := EncodeValues(in)
	if err != nil {
		t.Fatal(err)
	}
	want := "a=19&age=18&b=true&bool=false&byte=MTIz&bytes=MTIz&count=3&d=22.22&double=12.33&duration=2m0.000000022s&field=1%2C2&float=12.34&id=2233&int32=32&int64=64&map%5Bkratos%5D=https%3A%2F%2Fgo-kratos.dev%2F&map%5Bkratos_start%5D=https%3A%2F%2Fgo-kratos.dev%2Fen%2Fdocs%2Fgetting-started%2Fstart%2F&numberOne=2233&price=11.23&sex=woman&simples=3344&simples=5566&string=go-kratos&timestamp=1970-01-01T00%3A00%3A20.000000002Z&uint32=32&uint64=64&very_simple.component=5566"
	if got := query.Encode(); want != got {
		t.Errorf("want: %s, got: %s", want, got)
	}
}

I hope this helps! If you need further clarification, please let me know.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
1 participant