github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/api/server/runner_async_test.go (about) 1 // +build server 2 3 package server 4 5 import ( 6 "bytes" 7 "context" 8 "fmt" 9 "net/http" 10 "sync" 11 "testing" 12 13 "github.com/gin-gonic/gin" 14 "github.com/iron-io/functions/api/datastore" 15 "github.com/iron-io/functions/api/models" 16 "github.com/iron-io/functions/api/mqs" 17 "github.com/iron-io/functions/api/runner" 18 "github.com/iron-io/functions/api/runner/task" 19 "github.com/iron-io/functions/api/server/internal/routecache" 20 ) 21 22 func testRouterAsync(ds models.Datastore, mq models.MessageQueue, rnr *runner.Runner, tasks chan task.Request, enqueue models.Enqueue) *gin.Engine { 23 ctx := context.Background() 24 25 s := &Server{ 26 Runner: rnr, 27 Router: gin.New(), 28 Datastore: ds, 29 MQ: mq, 30 tasks: tasks, 31 Enqueue: enqueue, 32 hotroutes: routecache.New(10), 33 } 34 35 r := s.Router 36 r.Use(gin.Logger()) 37 38 r.Use(prepareMiddleware(ctx)) 39 s.bindHandlers(ctx) 40 return r 41 } 42 43 func TestRouteRunnerAsyncExecution(t *testing.T) { 44 tasks := mockTasksConduit() 45 ds := datastore.NewMockInit( 46 []*models.App{ 47 {Name: "myapp", Config: map[string]string{"app": "true"}}, 48 }, 49 []*models.Route{ 50 {Type: "async", Path: "/myroute", AppName: "myapp", Image: "iron/hello", Config: map[string]string{"test": "true"}}, 51 {Type: "async", Path: "/myerror", AppName: "myapp", Image: "iron/error", Config: map[string]string{"test": "true"}}, 52 {Type: "async", Path: "/myroute/:param", AppName: "myapp", Image: "iron/hello", Config: map[string]string{"test": "true"}}, 53 }, 54 ) 55 mq := &mqs.Mock{} 56 57 for i, test := range []struct { 58 path string 59 body string 60 headers map[string][]string 61 expectedCode int 62 expectedEnv map[string]string 63 }{ 64 {"/r/myapp/myroute", ``, map[string][]string{}, http.StatusAccepted, map[string]string{"TEST": "true", "APP": "true"}}, 65 // FIXME: this just hangs 66 //{"/r/myapp/myroute/1", ``, map[string][]string{}, http.StatusAccepted, map[string]string{"TEST": "true", "APP": "true"}}, 67 {"/r/myapp/myerror", ``, map[string][]string{}, http.StatusAccepted, map[string]string{"TEST": "true", "APP": "true"}}, 68 {"/r/myapp/myroute", `{ "name": "test" }`, map[string][]string{}, http.StatusAccepted, map[string]string{"TEST": "true", "APP": "true"}}, 69 { 70 "/r/myapp/myroute", 71 ``, 72 map[string][]string{"X-Function": []string{"test"}}, 73 http.StatusAccepted, 74 map[string]string{ 75 "TEST": "true", 76 "APP": "true", 77 "HEADER_X_FUNCTION": "test", 78 }, 79 }, 80 } { 81 body := bytes.NewBuffer([]byte(test.body)) 82 var wg sync.WaitGroup 83 84 wg.Add(1) 85 fmt.Println("About to start router") 86 rnr, cancel := testRunner(t) 87 router := testRouterAsync(ds, mq, rnr, tasks, func(_ context.Context, _ models.MessageQueue, task *models.Task) (*models.Task, error) { 88 if test.body != task.Payload { 89 t.Errorf("Test %d: Expected task Payload to be the same as the test body", i) 90 } 91 92 if test.expectedEnv != nil { 93 for name, value := range test.expectedEnv { 94 taskName := name 95 if value != task.EnvVars[taskName] { 96 t.Errorf("Test %d: Expected header `%s` to be `%s` but was `%s`", 97 i, name, value, task.EnvVars[taskName]) 98 } 99 } 100 } 101 102 wg.Done() 103 return task, nil 104 }) 105 106 fmt.Println("makeing requests") 107 req, rec := newRouterRequest(t, "POST", test.path, body) 108 for name, value := range test.headers { 109 req.Header.Set(name, value[0]) 110 } 111 fmt.Println("About to start router2") 112 router.ServeHTTP(rec, req) 113 fmt.Println("after servehttp") 114 115 if rec.Code != test.expectedCode { 116 t.Errorf("Test %d: Expected status code to be %d but was %d", 117 i, test.expectedCode, rec.Code) 118 } 119 120 wg.Wait() 121 cancel() 122 } 123 }