Skip to content

Commit

Permalink
Merge pull request gotomicro#227 from askuy/feature/unittest20211122
Browse files Browse the repository at this point in the history
add job unittest
  • Loading branch information
askuy committed Nov 22, 2021
2 parents da108e2 + 7ad084c commit 3829e48
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 2 deletions.
1 change: 0 additions & 1 deletion task/ejob/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ func TestComponent_new(t *testing.T) {
comp := newComponent("test-cmp", defaultConfig(), elog.EgoLogger)
assert.Equal(t, "test-cmp", comp.Name())
assert.Equal(t, "task.ejob", comp.PackageName())

}
6 changes: 5 additions & 1 deletion task/ejob/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,17 @@ func Job(name string, startFunc func(Context) error) *Component {
func Handle(w http.ResponseWriter, r *http.Request) {
jobName := r.Header.Get("X-Ego-Job-Name")
if jobName == "" {
w.Header().Set("X-Ego-Job-Err", "jobName not exist")
w.WriteHeader(400)
return
}
w.Header().Set("X-Ego-Job-Name", jobName)

//
jobRunID := r.Header.Get("X-Ego-Job-RunID")
if jobName == "" {
if jobRunID == "" {
w.Header().Set("X-Ego-Job-Err", fmt.Sprintf("jobName: %s, jobRunID not exist", jobName))
w.WriteHeader(400)
return
}
w.Header().Set("X-Ego-Job-RunID", jobRunID)
Expand Down
104 changes: 104 additions & 0 deletions task/ejob/container_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package ejob

import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
)

func TestJob(t *testing.T) {
fc := func(ctx Context) error {
return nil
}
Job("test", fc)
storeCache.RLock()
assert.Equal(t, 1, len(storeCache.cache))
storeCache.RUnlock()
}
func TestHandle(t *testing.T) {

fc := func(ctx Context) error {
return nil
}
Job("test", fc)
storeCache.RLock()
assert.Equal(t, 1, len(storeCache.cache))
storeCache.RUnlock()
mux := http.NewServeMux()
mux.HandleFunc("/jobs", Handle)
ts := httptest.NewServer(mux)
defer ts.Close()
{
resp := performRequest(mux, "POST", "/jobs")
assert.Equal(t, 400, resp.Code)
assert.Equal(t, "jobName not exist", resp.Header().Get("X-Ego-Job-Err"))
}
{
resp := performRequest(mux, "POST", "/jobs", header{
Key: "X-Ego-Job-Name",
Value: "test",
})
assert.Equal(t, 400, resp.Code)
assert.Equal(t, "jobName: test, jobRunID not exist", resp.Header().Get("X-Ego-Job-Err"))
}
{
resp := performRequest(mux, "POST", "/jobs", header{
Key: "X-Ego-Job-Name",
Value: "test1",
}, header{
Key: "X-Ego-Job-RunID",
Value: "123",
})
assert.Equal(t, 400, resp.Code)
assert.Equal(t, "job:test1 not exist", resp.Header().Get("X-Ego-Job-Err"))
}

{
resp := performRequest(mux, "POST", "/jobs", header{
Key: "X-Ego-Job-Name",
Value: "test",
}, header{
Key: "X-Ego-Job-RunID",
Value: "123",
})
assert.Equal(t, 200, resp.Code)
}

}

func TestHandleJobList(t *testing.T) {
fc := func(ctx Context) error {
return nil
}
Job("test", fc)
mux := http.NewServeMux()
mux.HandleFunc("/jobList", HandleJobList)
ts := httptest.NewServer(mux)
defer ts.Close()
resp := performRequest(mux, "GET", "/jobList")
jobList := make([]string, 0)
bytes, err := ioutil.ReadAll(resp.Body)
assert.Nil(t, err)
err = json.Unmarshal(bytes, &jobList)
assert.Nil(t, err)
assert.Equal(t, "test", jobList[0])
}

type header struct {
Key string
Value string
}

func performRequest(r http.Handler, method, path string, headers ...header) *httptest.ResponseRecorder {
req := httptest.NewRequest(method, path, nil)
for _, h := range headers {
req.Header.Add(h.Key, h.Value)
}
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
}
25 changes: 25 additions & 0 deletions task/ejob/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ejob

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestWithName(t *testing.T) {
container := DefaultContainer()
container.Build(WithName("test"))
assert.Equal(t, "test", container.config.Name)
}

func TestWithStartFunc(t *testing.T) {
container := DefaultContainer()
fc := func(ctx Context) error {
return nil
}
one := fmt.Sprintf("%p", fc)
container.Build(WithStartFunc(fc))
two := fmt.Sprintf("%p", container.config.startFunc)
assert.Equal(t, one, two)
}

0 comments on commit 3829e48

Please sign in to comment.