mirror of
https://github.com/google/nomulus.git
synced 2025-05-03 13:37:51 +02:00
ModulesService does not provide a great API. Specifically, it doesn't have a way to get the hostname for a specific service; you have to get the hostname for a specific version as well. This is very rarely what we want, as we publish new versions every week and don't expect old ones to hang around for very long, so a task should execute against whatever the live version is, not whatever the current version was back when the task was enqueued (especially because that version might be deleted by now). This new and improved wrapper API removes the confusion and plays better with dependency injection to boot. We can also fold in other methods having to do with App Engine services, whereas ModulesService was quite limited in scope. This also has the side effect of fixing ResaveEntityAction, which is currently broken because the tasks it's enqueuing to execute up to 30 days in the future have the version hard-coded into the hostname, and we typically delete old versions sooner than that. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=206173763
111 lines
3.8 KiB
Java
111 lines
3.8 KiB
Java
// Copyright 2017 The Nomulus Authors. 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.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License 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.
|
|
|
|
package google.registry.monitoring.whitebox;
|
|
|
|
import static com.google.appengine.api.taskqueue.QueueFactory.getQueue;
|
|
import static google.registry.bigquery.BigqueryUtils.toBigqueryTimestamp;
|
|
import static google.registry.monitoring.whitebox.BigQueryMetricsEnqueuer.QUEUE_BIGQUERY_STREAMING_METRICS;
|
|
import static google.registry.testing.TaskQueueHelper.assertTasksEnqueued;
|
|
import static org.mockito.Mockito.when;
|
|
|
|
import com.google.api.services.bigquery.model.TableFieldSchema;
|
|
import com.google.auto.value.AutoValue;
|
|
import com.google.common.base.Suppliers;
|
|
import com.google.common.collect.ImmutableList;
|
|
import com.google.common.collect.ImmutableMap;
|
|
import google.registry.testing.AppEngineRule;
|
|
import google.registry.testing.MockitoJUnitRule;
|
|
import google.registry.testing.TaskQueueHelper.TaskMatcher;
|
|
import google.registry.util.AppEngineServiceUtils;
|
|
import org.joda.time.DateTime;
|
|
import org.junit.Before;
|
|
import org.junit.Rule;
|
|
import org.junit.Test;
|
|
import org.junit.runner.RunWith;
|
|
import org.junit.runners.JUnit4;
|
|
import org.mockito.Mock;
|
|
|
|
/** Unit tests for {@link BigQueryMetricsEnqueuer}. */
|
|
@RunWith(JUnit4.class)
|
|
public class BigQueryMetricsEnqueuerTest {
|
|
|
|
@Rule
|
|
public final AppEngineRule appEngine =
|
|
AppEngineRule.builder().withDatastore().withLocalModules().withTaskQueue().build();
|
|
|
|
@Rule public final MockitoJUnitRule mocks = MockitoJUnitRule.create();
|
|
|
|
@Mock private AppEngineServiceUtils appEngineServiceUtils;
|
|
|
|
private BigQueryMetricsEnqueuer enqueuer;
|
|
|
|
@Before
|
|
public void setUp() {
|
|
enqueuer = new BigQueryMetricsEnqueuer();
|
|
enqueuer.idGenerator = Suppliers.ofInstance("laffo");
|
|
enqueuer.appEngineServiceUtils = appEngineServiceUtils;
|
|
enqueuer.queue = getQueue(QUEUE_BIGQUERY_STREAMING_METRICS);
|
|
when(appEngineServiceUtils.getCurrentVersionHostname("backend"))
|
|
.thenReturn("backend.test.localhost");
|
|
}
|
|
|
|
@Test
|
|
public void testExport() {
|
|
TestMetric metric =
|
|
TestMetric.create(
|
|
DateTime.parse("1984-12-18TZ"), DateTime.parse("1984-12-18TZ").plusMillis(1));
|
|
|
|
enqueuer.export(metric);
|
|
|
|
assertTasksEnqueued("bigquery-streaming-metrics",
|
|
new TaskMatcher()
|
|
.url("/_dr/task/metrics")
|
|
.header("Host", "backend.test.localhost")
|
|
.param("tableId", "test")
|
|
.param("startTime", "472176000.000000")
|
|
.param("endTime", "472176000.001000")
|
|
.param("insertId", "laffo"));
|
|
}
|
|
|
|
/** A stub implementation of {@link BigQueryMetric}. */
|
|
@AutoValue
|
|
abstract static class TestMetric implements BigQueryMetric {
|
|
|
|
static TestMetric create(DateTime startTimestamp, DateTime endTimestamp) {
|
|
return new AutoValue_BigQueryMetricsEnqueuerTest_TestMetric(startTimestamp, endTimestamp);
|
|
}
|
|
|
|
@Override
|
|
public String getTableId() {
|
|
return "test";
|
|
}
|
|
|
|
@Override
|
|
public ImmutableList<TableFieldSchema> getSchemaFields() {
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public ImmutableMap<String, String> getBigQueryRowEncoding() {
|
|
return ImmutableMap.of(
|
|
"startTime", toBigqueryTimestamp(getStartTimestamp()),
|
|
"endTime", toBigqueryTimestamp(getEndTimestamp()));
|
|
}
|
|
|
|
abstract DateTime getStartTimestamp();
|
|
|
|
abstract DateTime getEndTimestamp();
|
|
}
|
|
}
|