Skip to content

Commit

Permalink
Merge pull request #701 from mockerzzz/pdr-7062
Browse files Browse the repository at this point in the history
refactor http reader && http sender test
  • Loading branch information
wonderflow authored Aug 10, 2018
2 parents 7051744 + 7b15946 commit 2d7bf6f
Show file tree
Hide file tree
Showing 2 changed files with 227 additions and 103 deletions.
61 changes: 48 additions & 13 deletions reader/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,47 @@ import (
. "github.com/qiniu/logkit/utils/models"
)

func TestNewHttpReader(t *testing.T) {
var testData []string

func getHttpReader() (*Reader, error) {
readConf := conf.MapConf{
reader.KeyMetaPath: MetaDir,
reader.KeyFileDone: MetaDir,
reader.KeyMode: reader.ModeHTTP,
KeyRunnerName: "TestNewHttpReader",
}
meta, err := reader.NewMetaWithConf(readConf)
assert.NoError(t, err)
defer os.RemoveAll("./meta")
if err != nil {
return nil, err
}

c := conf.MapConf{
reader.KeyHTTPServiceAddress: "127.0.0.1:7110",
reader.KeyHTTPServicePath: "/logkit/aaa,/logkit/bbb,/logkit/ccc,/logkit/ddd",
}
hhttpReader, err := NewReader(meta, c)
reader, err := NewReader(meta, c)
httpReader := reader.(*Reader)
if err != nil {
return nil, err
}
err = httpReader.Start()
if err != nil {
return nil, err
}
return httpReader, nil
}

func TestNewHttpReader(t *testing.T) {
httpReader, err := getHttpReader()
assert.NoError(t, err)
httpReader := hhttpReader.(*Reader)
assert.NoError(t, httpReader.Start())
defer httpReader.Close()
defer func() {
os.RemoveAll("./meta")
httpReader.Close()
}()

// CI 环境启动监听较慢,需要等待几秒
time.Sleep(3 * time.Second)

testData := []string{
"1234567890987654321",
"qwertyuiopoiuytrewq",
"zxcvbnm,./.,mnbvcxz",
"asdfghjkl;';lkjhgfdsa",
}
paths := strings.Split("/logkit/aaa,/logkit/bbb,/logkit/ccc,/logkit/ddd", ",")

// 测试正常发送
Expand All @@ -70,8 +81,23 @@ func TestNewHttpReader(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.StatusCode)
wg.Wait()
}
}

func TestNewHttpReaderWithGzip(t *testing.T) {
httpReader, err := getHttpReader()
assert.NoError(t, err)
defer func() {
os.RemoveAll("./meta")
httpReader.Close()
}()

// CI 环境启动监听较慢,需要等待几秒
time.Sleep(3 * time.Second)

paths := strings.Split("/logkit/aaa,/logkit/bbb,/logkit/ccc,/logkit/ddd", ",")

// 测试 gzip 发送
var wg sync.WaitGroup
for index, val := range testData {
req, err := http.NewRequest(http.MethodPost, "http://127.0.0.1:7110"+paths[index], nil)
req.Header.Set(ContentTypeHeader, ApplicationGzip)
Expand All @@ -98,3 +124,12 @@ func TestNewHttpReader(t *testing.T) {
wg.Wait()
}
}

func init() {
testData = []string{
"1234567890987654321",
"qwertyuiopoiuytrewq",
"zxcvbnm,./.,mnbvcxz",
"asdfghjkl;';lkjhgfdsa",
}
}
Loading

0 comments on commit 2d7bf6f

Please sign in to comment.