forked from aws/aws-sdk-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKinesisTests.cpp
156 lines (136 loc) · 5.89 KB
/
KinesisTests.cpp
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include <aws/external/gtest.h>
#include <aws/core/client/ClientConfiguration.h>
#include <aws/core/utils/UUID.h>
#include <aws/core/utils/logging/LogMacros.h>
#include <aws/kinesis/KinesisClient.h>
#include <aws/kinesis/model/CreateStreamRequest.h>
#include <aws/kinesis/model/DescribeStreamRequest.h>
#include <aws/kinesis/model/RegisterStreamConsumerRequest.h>
#include <aws/kinesis/model/DeregisterStreamConsumerRequest.h>
#include <aws/kinesis/model/SubscribeToShardRequest.h>
#include <aws/kinesis/model/SubscribeToShardHandler.h>
#include <aws/kinesis/model/DescribeStreamConsumerRequest.h>
#include <aws/kinesis/model/ListShardsRequest.h>
#include <aws/testing/TestingEnvironment.h>
#include <aws/core/utils/Outcome.h>
#include <thread>
#include <chrono>
using namespace Aws;
using namespace Aws::Kinesis;
using namespace Aws::Kinesis::Model;
namespace {
const char ALLOC_TAG[] = "KinesisIntegrationTest";
// Creating the stream at the beginning of the test takes a long time (~ 1 minute)
// Since we're relying on pre-created Kinesis streams for Lambda's integration tests, we can use the same stream in this
// test.
// TODO: Create the streams as part of the setup and delete them during teardown. Needs to be done here and in Lambda's
// integration tests.
const char STREAM_NAME[] = "AWSNativeSDKIntegrationTest";
class KinesisTest : public ::testing::Test
{
protected:
void SetUp()
{
m_UUID = Aws::Utils::UUID::RandomUUID();
Client::ClientConfiguration config;
config.region = Aws::Region::US_EAST_1;
m_client.reset(Aws::New<KinesisClient>(ALLOC_TAG, config));
}
Aws::String BuildResourceName(const char* name)
{
return Aws::Testing::GetAwsResourcePrefix() + m_UUID + name;
}
void WaitUntilConsumerIsActive(const Aws::String& consumerARN)
{
DescribeStreamConsumerRequest request;
request.SetConsumerARN(consumerARN);
int maxRetries = 18;
while(maxRetries-- > 0)
{
const auto& outcome = m_client->DescribeStreamConsumer(request);
if (outcome.IsSuccess())
{
if (outcome.GetResult().GetConsumerDescription().GetConsumerStatus() == ConsumerStatus::ACTIVE)
{
AWS_LOGSTREAM_INFO(ALLOC_TAG, "Consumer " << consumerARN << " is now in ACTIVE state.");
return;
}
}
else if (outcome.GetError().GetErrorType() != KinesisErrors::RESOURCE_NOT_FOUND)
{
FAIL();
}
std::this_thread::sleep_for(std::chrono::seconds(10));
}
ASSERT_GT(maxRetries, 0);
}
Aws::UniquePtr<KinesisClient> m_client;
Aws::String m_UUID;
};
TEST_F(KinesisTest, EnhancedFanOut)
{
// Get the Stream ARN (different between accounts)
DescribeStreamRequest describeStreamRequest;
describeStreamRequest.SetStreamName(STREAM_NAME);
auto describeStreamOutcome = m_client->DescribeStream(describeStreamRequest);
ASSERT_TRUE(describeStreamOutcome.IsSuccess());
const auto streamARN = describeStreamOutcome.GetResult().GetStreamDescription().GetStreamARN();
// Register a consumer for enhanced fan-out
RegisterStreamConsumerRequest registerRequest;
const auto consumerName = BuildResourceName("sdktest");
registerRequest.WithConsumerName(consumerName).WithStreamARN(streamARN);
auto registerConsumerOutcome = m_client->RegisterStreamConsumer(registerRequest);
ASSERT_TRUE(registerConsumerOutcome.IsSuccess());
const auto consumerARN = registerConsumerOutcome.GetResult().GetConsumer().GetConsumerARN();
WaitUntilConsumerIsActive(consumerARN);
// Get the shard id
ListShardsRequest listShardRequest;
listShardRequest.SetStreamName(STREAM_NAME);
auto listShardsOutcome = m_client->ListShards(listShardRequest);
ASSERT_TRUE(listShardsOutcome.IsSuccess());
const auto& shards = listShardsOutcome.GetResult().GetShards();
ASSERT_FALSE(shards.empty());
const auto shardId = shards[0].GetShardId();
// Subscribe To the shard
SubscribeToShardHandler handler;
handler.SetSubscribeToShardEventCallback([](SubscribeToShardEvent const& ev)
{
const auto& records = ev.GetRecords();
AWS_LOGSTREAM_INFO(ALLOC_TAG, "Received " << records.size() << " records.");
});
handler.SetOnErrorCallback([](const Aws::Client::AWSError<KinesisErrors>& error)
{
AWS_LOGSTREAM_ERROR(ALLOC_TAG, "OnErrorCallback: " << error.GetMessage());
FAIL();
});
StartingPosition position;
position.SetType(ShardIteratorType::TRIM_HORIZON); // start from the beginning of the stream
SubscribeToShardRequest subscribeRequest;
subscribeRequest.WithConsumerARN(consumerARN)
.WithShardId(shardId)
.WithStartingPosition(position);
subscribeRequest.SetEventStreamHandler(handler);
const auto subscribeOutcome = m_client->SubscribeToShard(subscribeRequest);
ASSERT_TRUE(subscribeOutcome.IsSuccess());
// Deregister the consumer from fan-out (we're only allowed 5, so we must cleanup)
DeregisterStreamConsumerRequest deregisterRequest;
deregisterRequest.WithConsumerName(consumerName)
.WithStreamARN(streamARN)
.WithConsumerARN(consumerARN);
ASSERT_TRUE(m_client->DeregisterStreamConsumer(deregisterRequest).IsSuccess());
}
}