-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathawait_shutdown_test.go
More file actions
62 lines (46 loc) · 1.55 KB
/
await_shutdown_test.go
File metadata and controls
62 lines (46 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"context"
"firetail-lambda-extension/extensionsapi"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func getMockExtensionsApiServer() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `{"eventType": "SHUTDOWN"}`)
}))
}
func TestAwaitShutdownContextCancelled(t *testing.T) {
extensionClient := extensionsapi.NewClient()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
cancel()
reason, err := awaitShutdown(extensionClient, ctx)
assert.Nil(t, err)
assert.Equal(t, "context cancelled", reason)
}
func TestAwaitShutdownNextEventErrs(t *testing.T) {
extensionClient := extensionsapi.NewClient()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
reason, err := awaitShutdown(extensionClient, ctx)
require.NotNil(t, err)
assert.Contains(t, err.Error(), "failed to get next event")
assert.Equal(t, "", reason)
cancel()
}
func TestAwaitShutdownShutdownEvent(t *testing.T) {
mockExtensionsApi := getMockExtensionsApiServer()
defer mockExtensionsApi.Close()
t.Setenv("AWS_LAMBDA_RUNTIME_API", strings.Join(strings.Split(mockExtensionsApi.URL, ":")[1:], ":")[2:])
extensionClient := extensionsapi.NewClient()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
reason, err := awaitShutdown(extensionClient, ctx)
require.Nil(t, err)
assert.Equal(t, "received shutdown event", reason)
}