Skip to content

Commit

Permalink
Better programmatic generation of metadata json in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
QubitPi committed Nov 17, 2017
1 parent b2c28f1 commit d722376
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 252 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,30 @@ import org.redisson.config.Config

class RedisBroadcastChannelSpec extends BroadcastChannelSpec {


private static final SystemConfig SYSTEM_CONFIG = SystemConfigProvider.instance

boolean USE_REAL_REDIS_CLIENT = SYSTEM_CONFIG.getBooleanProperty(
SYSTEM_CONFIG.getPackageVariableName("use_real_redis_client"),
false
);
)

String REDIS_CHANNEL = SYSTEM_CONFIG.getStringProperty(
SYSTEM_CONFIG.getPackageVariableName("redisbroadcastchannel_name"),
"preResponse_notification_channel"
);
)

List<RedissonClient> redissonClients = new ArrayList<>()

List<MessageListener<String>> topicListeners = new ArrayList<>()

@Override
public BroadcastChannel<String> getBroadcastChannel() {
BroadcastChannel<String> getBroadcastChannel() {
RedissonClient redissonClient = USE_REAL_REDIS_CLIENT ? Redisson.create(createConfig()) : mockRedissonClient()
redissonClients << redissonClient
return new RedisBroadcastChannel<String>(redissonClient)
}

public RedissonClient mockRedissonClient() {
RedissonClient mockRedissonClient() {
RedissonClient redissonClient = Mock(RedissonClient)
RTopic<String> topic = Mock(RTopic)

Expand All @@ -55,7 +54,7 @@ class RedisBroadcastChannelSpec extends BroadcastChannelSpec {
return redissonClient
}

public static Config createConfig() {
static Config createConfig() {
// Redis server details
String redisHost = SYSTEM_CONFIG.getStringProperty(
SYSTEM_CONFIG.getPackageVariableName("redis_host"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,82 +20,129 @@ import io.druid.timeline.DataSegment
import spock.lang.Shared
import spock.lang.Specification

class BaseDataSourceMetadataSpec extends Specification {
/**
* Base specification of all metadata tests.
* <p>
* Any metadata Specs should extend this Spec, override childSetupSpec(), and call/override generate*() methods to
* initiate metadata related resources.
* <p>
* Take "intervals" as an example
* <ul>
* <li>
* you don't need any of the resources(including "intervals") defined in metadata tests, you do nothing and
* leave childSetupSpec() empty as it is in this Spec
* </li>
* <li>
* you need "intervals" in your test, you override childSetupSpec() and call its generation method in your Spec
* as follows
* <pre>
* {@code
* @Override
* def childSetupSpec() {
* intervals = generateIntervals()
* }
* }
* </pre>
* </li>
* <li>
* you need "intervals" in your test, but you need a different set of intervals:
* <pre>
* {@code
* @Override
* def childSetupSpec() {
* intervals = generateIntervals()
* }
*
* @Override
* Map<String, Interval> generateIntervals() {
* [
* "interval1": Interval.parse("2015-01-01T00:00:00.000Z/2015-01-02T00:00:00.000Z"),
* "interval2": Interval.parse("2015-01-02T00:00:00.000Z/2015-01-03T00:00:00.000Z"),
* "interval3": Interval.parse("2015-01-03T00:00:00.000Z/2015-01-04T00:00:00.000Z"),
* "interval123": Interval.parse("2015-01-01T00:00:00.000Z/2015-01-04T00:00:00.000Z")
* ]
* }
* }
* </pre>
* </li>
* </ul>
*/
abstract class BaseDataSourceMetadataSpec extends Specification {
@Shared
String tableName = TestDruidTableName.ALL_PETS.asName()
DateTimeZone currentTZ

@Shared
String intervalString1
@Shared
String intervalString2
String tableName
@Shared
String intervalString12

Map<String, Interval> intervals
@Shared
Interval interval1
RangeSet<DateTime> rangeSet
@Shared
Interval interval2
@Shared
Interval interval12

List<TestApiDimensionName> dimensions
@Shared
RangeSet<DateTime> rangeSet12

List<TestApiMetricName> metrics
@Shared
String version1
Integer binversion1
@Shared
String version2

long size1
@Shared
DataSegment segment1

long size2
@Shared
DataSegment segment2

List<String> versions
@Shared
DataSegment segment3
List<DataSegment> segments

@Shared
DataSegment segment4
def setupSpec() {
currentTZ = DateTimeZone.getDefault()
DateTimeZone.setDefault(DateTimeZone.UTC)
binversion1 = 9
size1 = 1024
size2 = 512

@Shared
List<DataSegment> segments
childSetupSpec()
}

@Shared
DateTimeZone currentTZ
def childSetupSpec() {}

@Shared
List<String> dimensions123
def shutdownSpec() {
DateTimeZone.setDefault(currentTZ)
}

@Shared
List<String> metrics123
String generateTableName() {
TestDruidTableName.ALL_PETS.asName()
}

def setupSpec() {
currentTZ = DateTimeZone.getDefault()
DateTimeZone.setDefault(DateTimeZone.UTC)
Map<String, Interval> generateIntervals() {
[
"interval1": Interval.parse("2015-01-01T00:00:00.000Z/2015-01-02T00:00:00.000Z"),
"interval2": Interval.parse("2015-01-02T00:00:00.000Z/2015-01-03T00:00:00.000Z"),
"interval12": Interval.parse("2015-01-01T00:00:00.000Z/2015-01-03T00:00:00.000Z")
]
}

intervalString1 = "2015-01-01T00:00:00.000Z/2015-01-02T00:00:00.000Z"
intervalString2 = "2015-01-02T00:00:00.000Z/2015-01-03T00:00:00.000Z"
intervalString12 = "2015-01-01T00:00:00.000Z/2015-01-03T00:00:00.000Z"
interval1 = Interval.parse(intervalString1)
interval2 = Interval.parse(intervalString2)
interval12 = Interval.parse(intervalString12)
rangeSet12 = TreeRangeSet.create()
rangeSet12.add(Range.closedOpen(interval12.getStart(), interval12.getEnd()))
version1 = DateTimeFormat.fullDateTime().print(DateTime.now().minusDays(1))
version2 = DateTimeFormat.fullDateTime().print(DateTime.now())
RangeSet<DateTime> generateRangeSet() {
rangeSet = TreeRangeSet.create()
rangeSet.add(Range.closedOpen(intervals["interval12"].getStart(), intervals["interval12"].getEnd()))
rangeSet
}

TestApiDimensionName dim1 = TestApiDimensionName.BREED
TestApiDimensionName dim2 = TestApiDimensionName.SPECIES
TestApiDimensionName dim3 = TestApiDimensionName.SEX
List<TestApiDimensionName> generateDimensions() {
[TestApiDimensionName.BREED, TestApiDimensionName.SPECIES, TestApiDimensionName.SEX]
}

dimensions123 = [dim1, dim2, dim3]*.asName()
List<TestApiMetricName> generateMetrics() {
[TestApiMetricName.A_ROW_NUM, TestApiMetricName.A_LIMBS, TestApiMetricName.A_DAY_AVG_LIMBS]
}

TestApiMetricName met1 = TestApiMetricName.A_ROW_NUM
TestApiMetricName met2 = TestApiMetricName.A_LIMBS
TestApiMetricName met3 = TestApiMetricName.A_DAY_AVG_LIMBS
List<String> generateVersions() {
[
DateTimeFormat.fullDateTime().print(DateTime.now().minusDays(1)),
DateTimeFormat.fullDateTime().print(DateTime.now())
]
}

metrics123 = [met1, met2, met3]*.asName()
List<DataSegment> generateSegments() {
versions = generateVersions()

NumberedShardSpec partition1 = Stub(NumberedShardSpec) {
getPartitionNum() >> 0
Expand All @@ -105,63 +152,51 @@ class BaseDataSourceMetadataSpec extends Specification {
getPartitionNum() >> 1
}

Integer binversion1 = 9
long size1 = 1024
long size2 = 512

segment1 = new DataSegment(
tableName,
interval1,
version1,
null,
dimensions123,
metrics123,
partition1,
binversion1,
size1
)

segment2 = new DataSegment(
tableName,
interval1,
version2,
null,
dimensions123,
metrics123,
partition2,
binversion1,
size2
)

segment3 = new DataSegment(
tableName,
interval2,
version1,
null,
dimensions123,
metrics123,
partition1,
binversion1,
size1
)

segment4 = new DataSegment(
tableName,
interval2,
version2,
null,
dimensions123,
metrics123,
partition2,
binversion1,
size2
)

segments = [segment1, segment2, segment3, segment4]

}

def shutdownSpec() {
DateTimeZone.setDefault(currentTZ)
[
new DataSegment(
tableName,
intervals["interval1"],
versions[0],
null,
dimensions*.asName(),
metrics*.asName(),
partition1,
binversion1,
size1
),
new DataSegment(
tableName,
intervals["interval1"],
versions[1],
null,
dimensions*.asName(),
metrics*.asName(),
partition2,
binversion1,
size2
),
new DataSegment(
tableName,
intervals["interval2"],
versions[0],
null,
dimensions*.asName(),
metrics*.asName(),
partition1,
binversion1,
size1
),
new DataSegment(
tableName,
intervals["interval2"],
versions[1],
null,
dimensions*.asName(),
metrics*.asName(),
partition2,
binversion1,
size2
)
]
}
}
Loading

0 comments on commit d722376

Please sign in to comment.