@Override public ThrottlingPolicy createPolicy(SharedLimiterKey key, SharedResourcesBroker<ThrottlingServerScopes> broker, Config config) { Preconditions.checkArgument(config.hasPath(COUNT_KEY), "Missing key " + COUNT_KEY); return new CountBasedPolicy(config.getLong(COUNT_KEY)); } }
@Test public void test() throws Exception { URL prefix = getClass().getResource("/configStore"); Config config = ConfigFactory.parseMap(ImmutableMap.of( ConfigClientBasedPolicyFactory.CONFIG_KEY_URI_PREFIX_KEY, "simple-" + prefix.toString() )); SharedResourcesBroker<ThrottlingServerScopes> broker = SharedResourcesBrokerFactory.createDefaultTopLevelBroker( ConfigFactory.empty(), ThrottlingServerScopes.GLOBAL.defaultScopeInstance()); ConfigClientBasedPolicyFactory policyFactory = new ConfigClientBasedPolicyFactory(); ThrottlingPolicy policy = policyFactory.createPolicy(new SharedLimiterKey("ConfigBasedPolicyTest/resource1"), broker, config); Assert.assertEquals(policy.getClass(), QPSPolicy.class); Assert.assertEquals(((QPSPolicy) policy).getQps(), 100); policy = policyFactory.createPolicy(new SharedLimiterKey("ConfigBasedPolicyTest/resource2"), broker, config); Assert.assertEquals(policy.getClass(), CountBasedPolicy.class); Assert.assertEquals(((CountBasedPolicy) policy).getCount(), 50); }
@Override public ThrottlingPolicy createPolicy(SharedLimiterKey key, SharedResourcesBroker<ThrottlingServerScopes> broker, Config config) { Preconditions.checkArgument(config.hasPath(COUNT_KEY), "Missing key " + COUNT_KEY); return new CountBasedPolicy(config.getLong(COUNT_KEY)); } }