Skip to content

Commit

Permalink
change to buffered channel
Browse files Browse the repository at this point in the history
  • Loading branch information
kapishmalik committed Apr 8, 2024
1 parent af59787 commit 91fcbc4
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 18 deletions.
1 change: 1 addition & 0 deletions core/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func (action *Action) Execute(pair *models.RequestResponsePair, journalIDChannel
time.Sleep(time.Duration(200+action.DelayInMs) * time.Millisecond)

journalID := <-journalIDChannel
close(journalIDChannel)
log.Info("Journal ID received ", journalID)

//if it is remote callback
Expand Down
9 changes: 4 additions & 5 deletions core/action/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ func Test_ExecuteLocalPostServeAction(t *testing.T) {
originalPair := models.RequestResponsePair{Response: resp, Request: req}

//not adding entry as update journal method will be tested in its file
journalIDChannel := make(chan string)
journalIDChannel := make(chan string, 1)
newJournal := journal.NewJournal()
journalIDChannel <- "1"
journalIDChannel <- "demo-id"
err = newAction.Execute(&originalPair, journalIDChannel, newJournal)
Expect(err).To(BeNil())
}
Expand All @@ -132,10 +132,9 @@ func Test_ExecuteRemotePostServeAction(t *testing.T) {
},
}
//not adding entry as update journal method will be tested in its file
journalIDChannel := make(chan string)
journalIDChannel := make(chan string, 1)
newJournal := journal.NewJournal()
journalIDChannel <- "1"

newAction, err := action.NewRemoteAction("test-callback", server.URL+"/process", 0)
Expect(err).To(BeNil())
err = newAction.Execute(&originalPair, journalIDChannel, newJournal)
Expand All @@ -153,7 +152,7 @@ func Test_ExecuteRemotePostServeAction_WithUnReachableHost(t *testing.T) {
Expect(err).To(BeNil())

//not adding entry as update journal method will be tested in its file
journalIDChannel := make(chan string)
journalIDChannel := make(chan string, 1)
newJournal := journal.NewJournal()
journalIDChannel <- "1"
err = newAction.Execute(&originalPair, journalIDChannel, newJournal)
Expand Down
2 changes: 1 addition & 1 deletion core/hoverfly.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (hf *Hoverfly) processRequest(req *http.Request) (*http.Response, chan stri

if result.PostServeActionInputDetails != nil {

journalIDChannel := make(chan string)
journalIDChannel := make(chan string, 1)
if postServeAction, ok := hf.PostServeActionDetails.Actions[result.PostServeActionInputDetails.PostServeAction]; ok {
go postServeAction.Execute(result.PostServeActionInputDetails.Pair, journalIDChannel, hf.Journal)
}
Expand Down
6 changes: 0 additions & 6 deletions core/hoverfly_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1371,12 +1371,6 @@ func TestHoverfly_GetPostServeActions(t *testing.T) {

Expect(postServeActions).NotTo(BeNil())
Expect(postServeActions.Actions).To(HaveLen(2))
Expect(postServeActions.Actions[0].ActionName).To(Equal("test-local-callback"))
Expect(postServeActions.Actions[0].Binary).To(Equal("python3"))
Expect(postServeActions.Actions[0].DelayInMs).To(Equal(1900))
Expect(postServeActions.Actions[1].ActionName).To(Equal("test-remote-callback"))
Expect(postServeActions.Actions[1].Remote).To(Equal("http://localhost"))
Expect(postServeActions.Actions[1].DelayInMs).To(Equal(1800))
}

func TestHoverfly_SetLocalPostServeAction(t *testing.T) {
Expand Down
14 changes: 8 additions & 6 deletions core/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ func NewProxy(hoverfly *Hoverfly) *goproxy.ProxyHttpServer {
startTime := time.Now()
resp, journalIDChannel := hoverfly.processRequest(r)
id, _ := hoverfly.Journal.NewEntry(r, resp, hoverfly.Cfg.Mode, startTime)
if journalIDChannel != nil {
journalIDChannel <- id
}
sendJournalIDToPostServeAction(journalIDChannel, id)
return r, resp
})

Expand Down Expand Up @@ -99,6 +97,12 @@ func NewProxy(hoverfly *Hoverfly) *goproxy.ProxyHttpServer {
return proxy
}

func sendJournalIDToPostServeAction(journalIDChannel chan string, id string) {
if journalIDChannel != nil {
journalIDChannel <- id
}
}

// Creates goproxy.ProxyHttpServer and configures it to be used as a webserver for Hoverfly
// goproxy is given a non proxy handler that uses the Hoverfly request processing
func NewWebserverProxy(hoverfly *Hoverfly) *goproxy.ProxyHttpServer {
Expand All @@ -109,9 +113,7 @@ func NewWebserverProxy(hoverfly *Hoverfly) *goproxy.ProxyHttpServer {
r.URL.Scheme = "http"
resp, journalIDChannel := hoverfly.processRequest(r)
id, _ := hoverfly.Journal.NewEntry(r, resp, hoverfly.Cfg.Mode, startTime)
if journalIDChannel != nil {
journalIDChannel <- id
}
sendJournalIDToPostServeAction(journalIDChannel, id)
body, err := util.GetResponseBody(resp)

if err != nil {
Expand Down

0 comments on commit 91fcbc4

Please sign in to comment.