Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Add unit test cases for download_api_test.go&algorithm_test.go&atomiccount_test.go&httpuils_test.go #1272

Merged
merged 1 commit into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions client/httpuils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,41 @@ import (

func TestParseHost(t *testing.T) {
tests := []struct {
name string
host string
expect error
name string
host string
expected error
}{
{
name: "http host",
host: "http://github.com",
expect: nil,
name: "tcp host",
host: "tcp://github.com",
expected: nil,
},
{
name: "https host",
host: "https://github.com",
expect: nil,
name: "unix host",
host: "unix://github.com",
expected: nil,
},
{
name: "not support url scheme",
host: "wss://github.com",
expect: errors.New("not support url scheme wss"),
name: "http host",
host: "http://github.com",
expected: nil,
},
{
name: "https host",
host: "https://github.com",
expected: nil,
},
{
name: "not support url scheme",
host: "wss://github.com",
expected: errors.New("not support url scheme wss"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, _, _, err := ParseHost(tt.host)
if (nil == err) != (nil == tt.expect) {
t.Errorf("expect: %v, got: %v\n", tt.expect, err)
if (nil == err) != (nil == tt.expected) {
t.Errorf("expected: %v, got: %v\n", tt.expected, err)
}
})
}
Expand Down
1 change: 1 addition & 0 deletions dfget/core/api/download_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (s *DownloadAPITestSuite) TestGetRealRange(c *check.C) {
rangeValue string
expected string
}{
{"", "0-1", ""},
{"0-1", "", "0-1"},
{"0-1", "1-100", "1-2"},
{"0-100", "1-100", "1-100"},
Expand Down
6 changes: 6 additions & 0 deletions pkg/atomiccount/atomiccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ func (suite *AtomicCountUtilSuite) TestAdd(c *check.C) {

result := acount.Get()
c.Check(result, check.Equals, (int32)(12))

var nilAcount *AtomicInt
c.Check(nilAcount.Add(5), check.Equals, (int32)(0))
Copy link
Member

@lowzj lowzj Apr 17, 2020

Choose a reason for hiding this comment

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

It makes me confused why there is no panic when invoking the function of nil struct pointer. After a while, I understand: nilAcount.Add(5) equals (*AtomicInt).Add(nilAcount, 5) in GoLang, and the implementation of function AtomicInt#Add checks nil pointer before dereference it.

But is it a reasonable implementation?

  • It's rare to check itself in a struct's own function. Why not let it panic directly?
  • The behavior of the AtomicInt functions are inconsistent: Set doesn't check.
  • Any value plus zero value should be equal to itself, but Add returns 0.

@starnop WDYT

}

func (suite *AtomicCountUtilSuite) TestSet(c *check.C) {
Expand All @@ -49,4 +52,7 @@ func (suite *AtomicCountUtilSuite) TestSet(c *check.C) {
_ = acount.Set(1)
result := acount.Get()
c.Check(result, check.Equals, (int32)(1))

var nilAcount *AtomicInt
c.Check(nilAcount.Get(), check.Equals, (int32)(0))
}