- body := ioutil.NopCloser(bytes.NewReader([]byte(`[{"message": {"data": "data"}}]`)))
- clientMock := mocks.HTTPClient{}
- clientMock.On("Get", mock.Anything).Return(&http.Response{
- StatusCode: http.StatusOK,
- Body: body,
- }, nil)
-
- clientMock.On("Do", mock.Anything).Return(&http.Response{
- StatusCode: http.StatusOK,
- }, nil)
-
- restclient.Client = &clientMock
-
- pollAndDistributeMessages("http://mrAddr")
-
- time.Sleep(100 * time.Millisecond)
-
- var actualRequest *http.Request
- clientMock.AssertCalled(t, "Get", "http://mrAddr/events/topic/users/dmaapmediatorproducer")
- clientMock.AssertNumberOfCalls(t, "Get", 1)
-
- clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
- actualRequest = req
- return true
- }))
- assertions.Equal(http.MethodPost, actualRequest.Method)
- assertions.Equal("consumerHost", actualRequest.URL.Host)
- assertions.Equal("/target", actualRequest.URL.Path)
- assertions.Equal("application/json; charset=utf-8", actualRequest.Header.Get("Content-Type"))
- actualBody, _ := ioutil.ReadAll(actualRequest.Body)
- assertions.Equal([]byte(`[{"message": {"data": "data"}}]`), actualBody)
- clientMock.AssertNumberOfCalls(t, "Do", 1)
+ wg.Add(2) // Two calls should be made to the server, one to poll and one to distribute
+ handlerUnderTest.pollAndDistributeMessages("http://mrAddr")
+
+ if waitTimeout(&wg, 100*time.Millisecond) {
+ t.Error("Not all calls to server were made")
+ t.Fail()
+ }
+}
+
+func TestHandlerAddJob_shouldAddJobToJobsMap(t *testing.T) {
+ assertions := require.New(t)
+
+ jobInfo := JobInfo{
+ InfoTypeIdentity: "type1",
+ InfoJobIdentity: "job1",
+ TargetUri: "http://consumerHost/target",
+ }
+
+ addCh := make(chan JobInfo)
+ handlerUnderTest := jobHandler{
+ mu: sync.Mutex{},
+ jobs: map[string]JobInfo{},
+ addJobCh: addCh,
+ }
+
+ go func() {
+ addCh <- jobInfo
+ }()
+
+ handlerUnderTest.monitorManagementChannels()
+
+ assertions.Len(handlerUnderTest.jobs, 1)
+ assertions.Equal(jobInfo, handlerUnderTest.jobs["job1"])
+}
+
+func TestHandlerDeleteJob_shouldDeleteJobFromJobsMap(t *testing.T) {
+ assertions := require.New(t)
+
+ deleteCh := make(chan string)
+ handlerUnderTest := jobHandler{
+ mu: sync.Mutex{},
+ jobs: map[string]JobInfo{"job1": {
+ InfoJobIdentity: "job1",
+ }},
+ deleteJobCh: deleteCh,
+ }
+
+ go func() {
+ deleteCh <- "job1"
+ }()
+
+ handlerUnderTest.monitorManagementChannels()
+
+ assertions.Len(handlerUnderTest.jobs, 0)
+}
+
+type RoundTripFunc func(req *http.Request) *http.Response
+
+func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
+ return f(req), nil
+}
+
+//NewTestClient returns *http.Client with Transport replaced to avoid making real calls
+func NewTestClient(fn RoundTripFunc) *http.Client {
+ return &http.Client{
+ Transport: RoundTripFunc(fn),
+ }
+}
+
+// waitTimeout waits for the waitgroup for the specified max timeout.
+// Returns true if waiting timed out.
+func waitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
+ c := make(chan struct{})
+ go func() {
+ defer close(c)
+ wg.Wait()
+ }()
+ select {
+ case <-c:
+ return false // completed normally
+ case <-time.After(timeout):
+ return true // timed out
+ }
+}
+
+func getBodyAsString(req *http.Request) string {
+ buf := new(bytes.Buffer)
+ buf.ReadFrom(req.Body)
+ return buf.String()