|
1 | 1 | package kinesis
|
2 | 2 |
|
3 |
| -import "testing" |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +const localEndpoint = "http://127.0.0.1:4567" |
4 | 12 |
|
5 | 13 | // Trivial test to make sure that Kinesis implements KinesisClient.
|
6 | 14 | func TestInterfaceIsImplemented(t *testing.T) {
|
@@ -40,3 +48,140 @@ func TestAddRecord(t *testing.T) {
|
40 | 48 | t.Errorf("%q != %q", len(args.Records), 1)
|
41 | 49 | }
|
42 | 50 | }
|
| 51 | + |
| 52 | +func TestListStreams(t *testing.T) { |
| 53 | + auth := &Auth{ |
| 54 | + AccessKey: "BAD_ACCESS_KEY", |
| 55 | + SecretKey: "BAD_SECRET_KEY", |
| 56 | + } |
| 57 | + |
| 58 | + client := NewWithEndpoint(auth, USEast1, localEndpoint) |
| 59 | + resp, err := client.ListStreams(NewArgs()) |
| 60 | + if resp == nil { |
| 61 | + t.Error("resp == nil") |
| 62 | + } |
| 63 | + if err != nil { |
| 64 | + t.Errorf("%q != nil", err) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestCreateStream(t *testing.T) { |
| 69 | + auth := &Auth{ |
| 70 | + AccessKey: "BAD_ACCESS_KEY", |
| 71 | + SecretKey: "BAD_SECRET_KEY", |
| 72 | + } |
| 73 | + |
| 74 | + client := NewWithEndpoint(auth, USEast1, localEndpoint) |
| 75 | + |
| 76 | + streamName := "test2" |
| 77 | + |
| 78 | + err := client.CreateStream(streamName, 1) |
| 79 | + if err != nil { |
| 80 | + t.Errorf("%q != nil", err) |
| 81 | + } |
| 82 | + |
| 83 | + err = waitForStreamStatus(client, streamName, "ACTIVE") |
| 84 | + if err != nil { |
| 85 | + t.Errorf("%q != nil", err) |
| 86 | + } |
| 87 | + |
| 88 | + client.DeleteStream(streamName) |
| 89 | + err = waitForStreamDeletion(client, streamName) |
| 90 | + if err != nil { |
| 91 | + t.Errorf("%q != nil", err) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestPutRecord(t *testing.T) { |
| 96 | + auth := &Auth{ |
| 97 | + AccessKey: "BAD_ACCESS_KEY", |
| 98 | + SecretKey: "BAD_SECRET_KEY", |
| 99 | + } |
| 100 | + |
| 101 | + client := NewWithEndpoint(auth, USEast1, localEndpoint) |
| 102 | + |
| 103 | + streamName := "pizza" |
| 104 | + err := createStream(client, streamName, 1) |
| 105 | + |
| 106 | + if err != nil { |
| 107 | + t.Errorf("%q != nil", err) |
| 108 | + } |
| 109 | + |
| 110 | + args := NewArgs() |
| 111 | + args.Add("StreamName", streamName) |
| 112 | + args.AddData([]byte("The cheese is old and moldy, where is the bathroom?")) |
| 113 | + args.Add("PartitionKey", "key-1") |
| 114 | + |
| 115 | + resp, err := client.PutRecord(args) |
| 116 | + if resp == nil { |
| 117 | + t.Error("resp == nil") |
| 118 | + } |
| 119 | + if err != nil { |
| 120 | + t.Errorf("%q != nil", err) |
| 121 | + } |
| 122 | + |
| 123 | + client.DeleteStream(streamName) |
| 124 | + err = waitForStreamDeletion(client, streamName) |
| 125 | + if err != nil { |
| 126 | + t.Errorf("%q != nil", err) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// waitForStreamStatus will poll for a stream status repeatedly, once every MS, for up to 1000 MS, |
| 131 | +// blocking until the stream has the desired status. It will return an error if the stream never |
| 132 | +// achieves the desired status. If a stream doesn’t exist then an error will be returned. |
| 133 | +func waitForStreamStatus(client *Kinesis, streamName string, statusToAwait string) error { |
| 134 | + args := NewArgs() |
| 135 | + args.Add("StreamName", streamName) |
| 136 | + var resp3 *DescribeStreamResp |
| 137 | + var err error |
| 138 | + |
| 139 | + for i := 1; i < 1000; i++ { |
| 140 | + resp3, err = client.DescribeStream(args) |
| 141 | + if err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + |
| 145 | + if resp3.StreamDescription.StreamStatus == statusToAwait { |
| 146 | + break |
| 147 | + } else { |
| 148 | + time.Sleep(1 * time.Millisecond) |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + if resp3 == nil { |
| 153 | + return errors.New("Could not get Stream Description") |
| 154 | + } |
| 155 | + |
| 156 | + if resp3.StreamDescription.StreamStatus != statusToAwait { |
| 157 | + return errors.New(fmt.Sprintf("Timed out waiting for stream to enter status %v; last status was %v.", statusToAwait, resp3.StreamDescription.StreamStatus)) |
| 158 | + } |
| 159 | + |
| 160 | + return nil |
| 161 | +} |
| 162 | + |
| 163 | +// waitForStreamDeletion will poll for a stream status repeatedly, once every MS, for up to 1000 MS, |
| 164 | +// blocking until the stream has been deleted. It will return an error if the stream is never deleted |
| 165 | +// or some other error occurs. If it succeeds then the return value will be nil. |
| 166 | +func waitForStreamDeletion(client *Kinesis, streamName string) error { |
| 167 | + err := waitForStreamStatus(client, streamName, "FOO") |
| 168 | + if !strings.Contains(err.Error(), "not found") { |
| 169 | + return err |
| 170 | + } |
| 171 | + return nil |
| 172 | +} |
| 173 | + |
| 174 | +// helper |
| 175 | +func createStream(client *Kinesis, streamName string, partitions int) error { |
| 176 | + err := client.CreateStream(streamName, partitions) |
| 177 | + if err != nil { |
| 178 | + return err |
| 179 | + } |
| 180 | + |
| 181 | + err = waitForStreamStatus(client, streamName, "ACTIVE") |
| 182 | + if err != nil { |
| 183 | + return err |
| 184 | + } |
| 185 | + |
| 186 | + return nil |
| 187 | +} |
0 commit comments