cloud.prefab.client.Tester Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of prefab-cloud-java Show documentation
Show all versions of prefab-cloud-java Show documentation
API Client for https://prefab.cloud: rate limits, feature flags and semaphores as a service
The newest version!
package cloud.prefab.client;
import cloud.prefab.client.value.Value;
import cloud.prefab.domain.Prefab;
import com.google.common.collect.Maps;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Executors;
public class Tester {
//
// @LiveConfig("key")
// private LiveString liveString;
// private static Value liveString;
public static void main(String[] args) throws InterruptedException, IOException {
System.out.println("A");
final PrefabCloudClient prefabCloudClient = new PrefabCloudClient(
new Options()
.setApikey("7-Production-P102-E108-SDK-577ed5ce-6faa-490b-ac41-cede1252eb22")
);
Value liveLong = prefabCloudClient.configClient().liveLong("intprop");
Value liveBoolean = prefabCloudClient.configClient().liveBoolean("boolprop");
while (true) {
System.out.println(
"live string is " +
liveLong.get() +
" and live boolean is " +
liveBoolean.get() +
" exp " +
prefabCloudClient.configClient().get("exp1.tester")
);
Thread.sleep(1000);
}
// liveString = FixedValue.of("foo");
// System.out.println("fixed string is " + liveString.get());
// net.spy.memcached.MemcachedClient memcachedClient = new net.spy.memcached.MemcachedClient(
// new InetSocketAddress("localhost", 11211));
// System.out.println("B");
// final RateLimitClient rateLimitClient = getLocalRateLimitClient(memcachedClient);
// testRateLimits(rateLimitClient);
// testConfig(prefabCloudClient, rateLimitClient);
// test();
}
private void onInitFail() {
PrefabCloudClient prefabCloudClient = new PrefabCloudClient(
new Options()
.setInitializationTimeoutSec(10)
.setOnInitializationFailure(Options.OnInitializationFailure.UNLOCK)
);
}
private void gettingStarted() {
PrefabCloudClient prefabCloudClient = new PrefabCloudClient(
new Options().setApikey("SDK-KEY, or set ENV var PREFAB_API_KEY")
);
FeatureFlagClient featureFlagClient = prefabCloudClient.featureFlagClient();
//true for 5 % of the users
// featureFlagClient.featureIsOnFor("features.example-flag", "123");
//
// // true because of the beta group rule
// featureFlagClient.featureIsOnFor(
// "features.example-flag",
// "123",
// Map.of("customer-group", "beta")
// );
}
PrefabCloudClient prefabCloudClient;
private void getConfig() {
final Optional configValue = prefabCloudClient
.configClient()
.get("the.key");
if (configValue.isPresent()) {
System.out.println(configValue.get().getString());
}
}
private void defaults() {
Options options = new Options()
.setNamespace("billing-service.jobs.dunning-job")
.setConfigOverrideDir(System.getProperty("user.home"))
.setApikey(System.getenv("PREFAB_API_KEY"))
.setPrefabDatasource(Options.Datasources.ALL) // Option: Datasources.LOCAL_ONLY
.setOnInitializationFailure(Options.OnInitializationFailure.RAISE) // Option Options.OnInitializationFailure.UNLOCK
.setInitializationTimeoutSec(10);
}
private static void test() throws InterruptedException {
System.out.println("C");
Options options = new Options()
.setNamespace("billing-service.jobs.dunning-job")
.setConfigOverrideDir(System.getProperty("user.home"))
.setApikey(System.getenv("PREFAB_API_KEY"))
.setPrefabDatasource(Options.Datasources.ALL) // Option: Datasources.LOCAL_ONLY
.setOnInitializationFailure(Options.OnInitializationFailure.RAISE) // Option Options.OnInitializationFailure.UNLOCK
.setInitializationTimeoutSec(10);
// .setPref()
//// .setApikey("50|99562dc4f19055d45551b7d2796074db");
// .setTarget("api.staging-prefab.cloud")
//// .setApikey("1|9991234jhlkk3");
// .setApikey("100-43760eb1caee9431d8f8b62e5a552880-Development-SDK");
System.out.println("D");
//
// PrefabCloudClient.Builder builder = new PrefabCloudClient.Builder()
// .setNamespace("foo")
// .setApikey("50|99562dc4f19055d45551b7d2796074db");
// .setTarget("localhost:8084")
// .setSsl(false)
// .setApikey("1|asdfjklhsfljk");
final PrefabCloudClient prefabCloudClient = new PrefabCloudClient(options);
// testRateLimits(prefabCloudClient);
System.out.println("E");
System.out.println(prefabCloudClient.getOptions().getApikey());
System.out.println(prefabCloudClient.getOptions().getPrefabApiUrl());
System.out.println(prefabCloudClient.getOptions().getCDNUrl());
System.out.println(prefabCloudClient.getOptions().getPrefabGrpcUrl());
// System.out.println(prefabCloudClient.configClient().getKeys());
// testConfig(prefabCloudClient, prefabCloudClient.rateLimitClient());
// testActual(prefabCloudClient);
}
private static void testActual(PrefabCloudClient prefabCloudClient) {
final RateLimitClient rateLimitClient = prefabCloudClient.rateLimitClient();
for (int i = 0; i < 10; ++i) {
final boolean result = rateLimitClient
.acquire(
Prefab.LimitRequest
.newBuilder()
.setAcquireAmount(1)
.addGroups("googleTranscriptionApi")
.build(),
Prefab.OnFailure.THROW
)
.getPassed();
System.out.println("acquire? " + result);
}
}
private static void testRateLimits(PrefabCloudClient prefabCloudClient) {
final RateLimitClient rateLimitClient = prefabCloudClient.rateLimitClient();
for (int i = 0; i < 100; ++i) {
final boolean result = rateLimitClient
.acquire(
// Prefab.LimitRequest.newBuilder().setAcquireAmount(1).addGroups("googleTranscriptionApi")
Prefab.LimitRequest
.newBuilder()
.setAcquireAmount(1)
.addGroups("2_per_sec")
// Prefab.LimitRequest.newBuilder().setAcquireAmount(1).addGroups("2_per_min")
.build(),
Prefab.OnFailure.THROW
)
.getPassed();
System.out.println("acquire? " + result);
}
}
// private static RateLimitClient getLocalRateLimitClient(MemcachedClient memcachedClient) {
// PrefabCloudClient.Builder builder = new PrefabCloudClient.Builder()
// .setNamespace("foo")
// .setDistributedCache(new MemcachedCache(memcachedClient));
//
// final PrefabCloudClient prefabCloudClient = new PrefabCloudClient(builder);
//
// return prefabCloudClient.rateLimitClient();
// }
private static void testRateLimits(
RateLimitClient rateLimitClient,
ConfigClient configClient
) throws InterruptedException {
configClient.upsert(
"hubtest.secondly",
Prefab.ConfigValue
.newBuilder()
.setLimitDefinition(
Prefab.LimitDefinition
.newBuilder()
.setLimit(1)
.setPolicyName(Prefab.LimitResponse.LimitPolicyNames.SECONDLY_ROLLING)
.build()
)
.build()
);
for (int i = 0; i < 40; i++) {
boolean result = rateLimitClient.isPass(
Prefab.LimitRequest
.newBuilder()
.setAcquireAmount(1)
.addGroups("hubtest.secondly")
.build()
);
Thread.sleep(100);
System.out.println("acquire? " + result);
}
}
private static void testConfig(
PrefabCloudClient prefabCloudClient3,
RateLimitClient rateLimitClient
) throws InterruptedException {
Options builder = new Options();
final PrefabCloudClient prefabCloudClient = new PrefabCloudClient(builder);
final ConfigClient configClient = prefabCloudClient.configClient();
System.out.println(String.format("config1 is %s", configClient.get("config1")));
// configClient.upsert(Prefab.UpsertRequest.newBuilder()
// .setConfigDelta(Prefab.ConfigDelta.newBuilder()
// .setKey("example")
// .setValue(Prefab.ConfigValue.newBuilder()
// .setInt(1)
// .build())
// .build())
// .build(), configValue);
System.out.println("F");
// configClient.upsert("example", Prefab.ConfigValue.newBuilder()
// .setInt(1)
// .build());
System.out.println(String.format("config1 is %s", configClient.get("config1"))); //returns 1
System.out.println("G");
}
private void other(PrefabCloudClient prefabCloudClient) throws InterruptedException {
final ConfigClient configClient = prefabCloudClient.configClient();
Executors
.newFixedThreadPool(1)
.submit(
new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
final long value = (long) (Math.random() * 10);
System.out.println("set to " + value);
configClient.upsert(
"A",
Prefab.ConfigValue.newBuilder().setInt(value).build()
);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
);
final FeatureFlagClient featureFlagClient = prefabCloudClient.featureFlagClient();
configClient.upsert(
"hourlytask",
Prefab.ConfigValue
.newBuilder()
.setLimitDefinition(
Prefab.LimitDefinition
.newBuilder()
.setLimit(1)
.setPolicyName(Prefab.LimitResponse.LimitPolicyNames.HOURLY_ROLLING)
.build()
)
.build()
);
// System.out.println("feature is on" + featureFlagClient.featureIsOn("A"));
for (int i = 0; i < 100; i++) {
System.out.println("Value is " + configClient.get("A"));
Thread.sleep(3000);
}
// Value vv = foo.getLive(Integer, "a");
//Create a flag that is on for 10 % of traffic, the entire beta group and user:
// configClient.upsert(
// Prefab.Config
// .newBuilder()
// .setKey("MyFeature")
// .addVariants(Prefab.FeatureFlagVariant.newBuilder().setBool(false).build())
// .addVariants(Prefab.FeatureFlagVariant.newBuilder().setBool(true).build())
// .addRows(
// Prefab.ConfigRow
// .newBuilder()
// .setValue(
// Prefab.ConfigValue
// .newBuilder()
// .setFeatureFlag(
// Prefab.FeatureFlag
// .newBuilder()
// .setActive(true)
// .setInactiveVariantIdx(0)
// .addRules(
// Prefab.Rule
// .newBuilder()
// .addVariantWeights(
// Prefab.VariantWeight
// .newBuilder()
// .setVariantIdx(0)
// .setWeight(500)
// .build()
// )
// .addVariantWeights(
// Prefab.VariantWeight
// .newBuilder()
// .setVariantIdx(1)
// .setWeight(500)
// .build()
// )
// .build()
// )
// .build()
// )
// .build()
// )
// .build()
// )
// .build()
// );
//
// //returns yes 10 pct of the time
// System.out.println(featureFlagClient.featureIsOn("MyFeature"));
//
// //A single user should get the same result each time
// //with 10 % probability user1123 will return yes, and if they do they always will
// System.out.println(
// featureFlagClient.featureIsOnFor("MyFeature", "user:1123", Maps.newHashMap())
// );
//
// Map attrs = Maps.newHashMap();
// attrs.put("group", "betas");
// //Utilize the whitelisted attributes to easily feature flag groups of people
// System.out.println(featureFlagClient.featureIsOnFor("MyFeature", "user:234", attrs));
}
}