Search in sources :

Example 6 with Timestamp

use of com.google.cloud.Timestamp in project google-cloud-java by GoogleCloudPlatform.

the class LoggingAppenderTest method testFilterLogsOnlyLogsAtOrAboveLogLevel.

@Test
public void testFilterLogsOnlyLogsAtOrAboveLogLevel() {
    LogEntry logEntry = LogEntry.newBuilder(StringPayload.of("this is a test")).setTimestamp(100000L).setSeverity(Severity.ERROR).setLabels(new ImmutableMap.Builder<String, String>().put("levelName", "ERROR").put("levelValue", String.valueOf(40000L)).build()).build();
    logging.setFlushSeverity(Severity.ERROR);
    Capture<Iterable<LogEntry>> capturedArgument = Capture.newInstance();
    logging.write(capture(capturedArgument), (WriteOption) anyObject(), (WriteOption) anyObject());
    expectLastCall().once();
    replay(logging);
    Timestamp timestamp = Timestamp.ofTimeSecondsAndNanos(100000, 0);
    LoggingEvent loggingEvent1 = createLoggingEvent(Level.INFO, timestamp.getSeconds());
    ThresholdFilter thresholdFilter = new ThresholdFilter();
    thresholdFilter.setLevel("ERROR");
    thresholdFilter.start();
    loggingAppender.addFilter(thresholdFilter);
    loggingAppender.start();
    // info event does not get logged
    loggingAppender.doAppend(loggingEvent1);
    LoggingEvent loggingEvent2 = createLoggingEvent(Level.ERROR, timestamp.getSeconds());
    // error event gets logged
    loggingAppender.doAppend(loggingEvent2);
    verify(logging);
    Assert.assertTrue(capturedArgument.getValue().iterator().hasNext());
    Assert.assertTrue(logEntry.equals(capturedArgument.getValue().iterator().next()));
}
Also used : LoggingEvent(ch.qos.logback.classic.spi.LoggingEvent) ThresholdFilter(ch.qos.logback.classic.filter.ThresholdFilter) Timestamp(com.google.cloud.Timestamp) LogEntry(com.google.cloud.logging.LogEntry) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 7 with Timestamp

use of com.google.cloud.Timestamp in project google-cloud-java by GoogleCloudPlatform.

the class LoggingAppenderTest method testEnhancersAddCorrectLabelsToLogEntries.

@Test
public void testEnhancersAddCorrectLabelsToLogEntries() {
    LogEntry logEntry = LogEntry.newBuilder(StringPayload.of("this is a test")).setTimestamp(100000L).setSeverity(Severity.WARNING).setLabels(new ImmutableMap.Builder<String, String>().put("levelName", "WARN").put("levelValue", String.valueOf(30000L)).put("test-label-1", "test-value-1").put("test-label-2", "test-value-2").build()).build();
    logging.setFlushSeverity(Severity.ERROR);
    Capture<Iterable<LogEntry>> capturedArgument = Capture.newInstance();
    logging.write(capture(capturedArgument), (WriteOption) anyObject(), (WriteOption) anyObject());
    expectLastCall().once();
    replay(logging);
    loggingAppender.addEnhancer("com.example.enhancers.TestLoggingEnhancer");
    loggingAppender.addEnhancer("com.example.enhancers.AnotherTestLoggingEnhancer");
    loggingAppender.start();
    Timestamp timestamp = Timestamp.ofTimeSecondsAndNanos(100000, 0);
    LoggingEvent loggingEvent = createLoggingEvent(Level.WARN, timestamp.getSeconds());
    loggingAppender.doAppend(loggingEvent);
    verify(logging);
    Assert.assertTrue(capturedArgument.getValue().iterator().hasNext());
    Assert.assertTrue(logEntry.equals(capturedArgument.getValue().iterator().next()));
}
Also used : LoggingEvent(ch.qos.logback.classic.spi.LoggingEvent) Timestamp(com.google.cloud.Timestamp) LogEntry(com.google.cloud.logging.LogEntry) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 8 with Timestamp

use of com.google.cloud.Timestamp in project google-cloud-java by GoogleCloudPlatform.

the class LoggingAppenderTest method testFlushLevelConfigUpdatesLoggingFlushSeverity.

@Test
public void testFlushLevelConfigUpdatesLoggingFlushSeverity() {
    LogEntry logEntry = LogEntry.newBuilder(StringPayload.of("this is a test")).setTimestamp(100000L).setSeverity(Severity.WARNING).setLabels(new ImmutableMap.Builder<String, String>().put("levelName", "WARN").put("levelValue", String.valueOf(30000L)).build()).build();
    logging.setFlushSeverity(Severity.WARNING);
    Capture<Iterable<LogEntry>> capturedArgument = Capture.newInstance();
    logging.write(capture(capturedArgument), (WriteOption) anyObject(), (WriteOption) anyObject());
    replay(logging);
    Timestamp timestamp = Timestamp.ofTimeSecondsAndNanos(100000, 0);
    LoggingEvent loggingEvent = createLoggingEvent(Level.WARN, timestamp.getSeconds());
    // error is the default, updating to warn for test
    loggingAppender.setFlushLevel(Level.WARN);
    loggingAppender.start();
    loggingAppender.doAppend(loggingEvent);
    verify(logging);
    Assert.assertTrue(capturedArgument.getValue().iterator().hasNext());
    Assert.assertTrue(logEntry.equals(capturedArgument.getValue().iterator().next()));
}
Also used : LoggingEvent(ch.qos.logback.classic.spi.LoggingEvent) Timestamp(com.google.cloud.Timestamp) LogEntry(com.google.cloud.logging.LogEntry) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 9 with Timestamp

use of com.google.cloud.Timestamp in project google-cloud-java by GoogleCloudPlatform.

the class BaseEntityTest method testGetTimestamp.

@Test
public void testGetTimestamp() throws Exception {
    BaseEntity<Key> entity = builder.build();
    assertEquals(TIMESTAMP, entity.getTimestamp("timestamp"));
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, -1);
    Timestamp timestamp = Timestamp.of(cal.getTime());
    entity = builder.set("timestamp", TimestampValue.of(timestamp)).build();
    assertEquals(timestamp, entity.getTimestamp("timestamp"));
}
Also used : Calendar(java.util.Calendar) Timestamp(com.google.cloud.Timestamp) Test(org.junit.Test)

Example 10 with Timestamp

use of com.google.cloud.Timestamp in project google-cloud-java by GoogleCloudPlatform.

the class ITTransactionTest method doBasicsTest.

private void doBasicsTest(final ReadStrategy strategy) throws InterruptedException {
    final String key = uniqueKey();
    // Initial value.
    client.write(Arrays.asList(Mutation.newInsertBuilder("T").set("K").to(key).set("V").to(0).build()));
    final int numThreads = 3;
    final CountDownLatch commitBarrier = new CountDownLatch(numThreads);
    final CountDownLatch complete = new CountDownLatch(numThreads);
    final TransactionCallable<Long> callable = new TransactionCallable<Long>() {

        @Override
        public Long run(TransactionContext transaction) throws SpannerException {
            Struct row = strategy.read(transaction, key);
            long newValue = row.getLong(0) + 1;
            transaction.buffer(Mutation.newUpdateBuilder("T").set("K").to(key).set("V").to(newValue).build());
            commitBarrier.countDown();
            // Synchronize so that all threads attempt to commit at the same time.
            Uninterruptibles.awaitUninterruptibly(commitBarrier);
            return newValue;
        }
    };
    // We start multiple threads all attempting to update the same value concurrently.  We expect
    // to see at least some of the corresponding transactions abort.
    final Vector<Long> results = new Vector<>();
    final Vector<Timestamp> commitTimestamps = new Vector<>();
    class TxnThread extends Thread {

        @Override
        public void run() {
            TransactionRunner runner = client.readWriteTransaction();
            Long result = runner.run(callable);
            results.add(result);
            commitTimestamps.add(runner.getCommitTimestamp());
            complete.countDown();
        }
    }
    for (int i = 0; i < numThreads; ++i) {
        new TxnThread().start();
    }
    complete.await();
    assertThat(results).hasSize(numThreads);
    List<Long> expectedResults = new ArrayList<>();
    for (int i = 0; i < numThreads; ++i) {
        expectedResults.add(i + 1L);
    }
    assertThat(results).containsAllIn(expectedResults);
    assertThat(Sets.newHashSet(commitTimestamps)).hasSize(numThreads);
    assertThat(client.singleUse(TimestampBound.strong()).readRow("T", Key.of(key), Arrays.asList("V")).getLong(0)).isEqualTo(Long.valueOf(numThreads));
}
Also used : TransactionCallable(com.google.cloud.spanner.TransactionRunner.TransactionCallable) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Timestamp(com.google.cloud.Timestamp) Struct(com.google.cloud.spanner.Struct) TransactionContext(com.google.cloud.spanner.TransactionContext) TransactionRunner(com.google.cloud.spanner.TransactionRunner) Vector(java.util.Vector)

Aggregations

Timestamp (com.google.cloud.Timestamp)22 Test (org.junit.Test)20 Struct (com.google.cloud.spanner.Struct)9 IntegrationTest (com.google.cloud.spanner.IntegrationTest)8 LoggingEvent (ch.qos.logback.classic.spi.LoggingEvent)4 LogEntry (com.google.cloud.logging.LogEntry)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Date (com.google.cloud.Date)2 Mutation (com.google.cloud.spanner.Mutation)2 ReadOnlyTransaction (com.google.cloud.spanner.ReadOnlyTransaction)2 TimestampBound (com.google.cloud.spanner.TimestampBound)2 EqualsTester (com.google.common.testing.EqualsTester)2 TreeMap (java.util.TreeMap)2 ThresholdFilter (ch.qos.logback.classic.filter.ThresholdFilter)1 WriteOption (com.google.cloud.logging.Logging.WriteOption)1 TransactionContext (com.google.cloud.spanner.TransactionContext)1 TransactionRunner (com.google.cloud.spanner.TransactionRunner)1 TransactionCallable (com.google.cloud.spanner.TransactionRunner.TransactionCallable)1 ByteString (com.google.protobuf.ByteString)1 CommitRequest (com.google.spanner.v1.CommitRequest)1