-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow to bind KafkaClientMetrics for each decaton consumer #132
Changes from all commits
7036afb
3fc39f1
1340444
aca08e0
92c4a5c
273b1a1
df09b3c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,15 +16,25 @@ | |
|
||
package com.linecorp.decaton.testing; | ||
|
||
import static com.linecorp.decaton.processor.runtime.ProcessorProperties.CONFIG_BIND_CLIENT_METRICS; | ||
|
||
import java.time.Duration; | ||
import java.util.Optional; | ||
import java.util.Properties; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.concurrent.locks.LockSupport; | ||
import java.util.function.BooleanSupplier; | ||
import java.util.function.Consumer; | ||
import java.util.function.Supplier; | ||
|
||
import com.linecorp.decaton.processor.runtime.ProcessorProperties; | ||
import com.linecorp.decaton.processor.runtime.Property; | ||
import com.linecorp.decaton.processor.runtime.PropertySupplier; | ||
import com.linecorp.decaton.processor.runtime.StaticPropertySupplier; | ||
import com.linecorp.decaton.processor.runtime.SubscriptionStateListener; | ||
import org.apache.kafka.clients.consumer.ConsumerConfig; | ||
import org.apache.kafka.clients.producer.KafkaProducer; | ||
|
@@ -62,6 +72,7 @@ private static Properties defaultProducerProps(String bootstrapServers) { | |
} | ||
|
||
public static final String DEFAULT_GROUP_ID = "test-group"; | ||
public static final Duration DEFINITELY_TOO_SLOW = Duration.ofSeconds(20); | ||
|
||
/** | ||
* A helper to instantiate {@link DecatonClient} for producing protobuf tasks with preset configurations | ||
|
@@ -116,7 +127,8 @@ public static Producer<String, DecatonTaskRequest> producer(String bootstrapServ | |
* @return {@link ProcessorSubscription} instance which is already running with unique subscription id assigned | ||
*/ | ||
public static ProcessorSubscription subscription(String bootstrapServers, | ||
Consumer<SubscriptionBuilder> builderConfigurer) { | ||
Consumer<SubscriptionBuilder> builderConfigurer) | ||
throws InterruptedException, TimeoutException { | ||
return subscription("subscription-" + sequence(), | ||
bootstrapServers, | ||
builderConfigurer); | ||
|
@@ -133,7 +145,8 @@ public static ProcessorSubscription subscription(String bootstrapServers, | |
*/ | ||
public static ProcessorSubscription subscription(String subscriptionId, | ||
String bootstrapServers, | ||
Consumer<SubscriptionBuilder> builderConfigurer) { | ||
Consumer<SubscriptionBuilder> builderConfigurer) | ||
throws InterruptedException, TimeoutException { | ||
AtomicReference<SubscriptionStateListener> stateListenerRef = new AtomicReference<>(); | ||
CountDownLatch initializationLatch = new CountDownLatch(1); | ||
SubscriptionStateListener outerStateListener = state -> { | ||
|
@@ -160,14 +173,12 @@ public SubscriptionBuilder stateListener(SubscriptionStateListener stateListener | |
|
||
builderConfigurer.accept(builder); | ||
builder.consumerConfig(props) | ||
.addProperties(StaticPropertySupplier.of(Property.ofStatic(CONFIG_BIND_CLIENT_METRICS, true))) | ||
.stateListener(outerStateListener); | ||
ProcessorSubscription subscription = builder.buildAndStart(); | ||
|
||
try { | ||
initializationLatch.await(); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new RuntimeException(e); | ||
if (!initializationLatch.await(DEFINITELY_TOO_SLOW.toMillis(), TimeUnit.MILLISECONDS)) { | ||
throw new TimeoutException("Initialization did not complete within " + DEFINITELY_TOO_SLOW); | ||
} | ||
return subscription; | ||
} | ||
|
@@ -178,7 +189,7 @@ public SubscriptionBuilder stateListener(SubscriptionStateListener stateListener | |
* @param condition expected condition to be met | ||
*/ | ||
public static void awaitCondition(String message, | ||
Supplier<Boolean> condition) { | ||
BooleanSupplier condition) { | ||
awaitCondition(message, condition, Long.MAX_VALUE); | ||
} | ||
|
||
|
@@ -189,20 +200,15 @@ public static void awaitCondition(String message, | |
* @param timeoutMillis max duration to wait | ||
*/ | ||
public static void awaitCondition(String message, | ||
Supplier<Boolean> condition, | ||
BooleanSupplier condition, | ||
long timeoutMillis) { | ||
long start = System.nanoTime(); | ||
while (!condition.get()) { | ||
while (!condition.getAsBoolean()) { | ||
long elapsedMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start); | ||
if (elapsedMillis >= timeoutMillis) { | ||
throw new AssertionError(message); | ||
} | ||
try { | ||
Thread.sleep(100L); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new RuntimeException(e); | ||
} | ||
LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(100)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the intention of this change? Seems like waiting just 100ms here isn't an actual problem? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just avoid having to deal with |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
curious why backtrace provided when test itself times out (
@Test(timeout = xxx)
) isn't sufficient to know at where it stuck?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because it was not exactly stuck. Producer and Consumer were running and doing nothing.