Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,20 @@
*/
public class StringQuoter {
private static final String ISO8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSz";
private static final DateFormat ISO8601 = new SimpleDateFormat(ISO8601_PATTERN, Locale
.getDefault());
private static final ThreadLocal<DateFormat> ISO8601 = new ThreadLocal<DateFormat>() {
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat(ISO8601_PATTERN, Locale.getDefault());
}
};

private static final String RFC2822_PATTERN = "EEE, d MMM yyyy HH:mm:ss Z";
private static final DateFormat RFC2822 = new SimpleDateFormat(RFC2822_PATTERN, Locale
.getDefault());
private static final ThreadLocal<DateFormat> RFC2822 = new ThreadLocal<DateFormat>() {
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat(RFC2822_PATTERN, Locale.getDefault());
}
};

public static Splittable create(boolean value) {
return JsonSplittable.create(String.valueOf(value));
Expand Down Expand Up @@ -85,11 +93,11 @@ public static Date tryParseDate(String date) {
date = date.substring(0, date.length() - 1) + "+0000";
}
try {
return ISO8601.parse(date);
return ISO8601.get().parse(date);
} catch (ParseException ignored) {
}
try {
return RFC2822.parse(date);
return RFC2822.get().parse(date);
} catch (ParseException ignored) {
}
return null;
Expand Down
2 changes: 2 additions & 0 deletions user/test/com/google/web/bindery/autobean/AutoBeanSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.web.bindery.autobean.vm.AutoBeanCodexJreTest;
import com.google.web.bindery.autobean.vm.AutoBeanJreTest;
import com.google.web.bindery.autobean.vm.SplittableJreTest;
import com.google.web.bindery.autobean.vm.StringQuoterJreTest;

import junit.framework.Test;

Expand All @@ -38,6 +39,7 @@ public static Test suite() {
suite.addTestSuite(AutoBeanTest.class);
suite.addTestSuite(SplittableJreTest.class);
suite.addTestSuite(SplittableTest.class);
suite.addTestSuite(StringQuoterJreTest.class);
return suite;
}
}
115 changes: 115 additions & 0 deletions user/test/com/google/web/bindery/autobean/vm/StringQuoterJreTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright 2026 Google Inc.
*
* 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 com.google.web.bindery.autobean.vm;

import com.google.web.bindery.autobean.shared.impl.StringQuoter;

import junit.framework.TestCase;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Tests for the server-side {@link StringQuoter#tryParseDate(String)}.
*/
public class StringQuoterJreTest extends TestCase {

private static final String ISO8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSz";
private static final String RFC2822_PATTERN = "EEE, d MMM yyyy HH:mm:ss Z";

public void testTryParseDateMillis() {
Date d = new Date(1234567890123L);
assertEquals(d, StringQuoter.tryParseDate(Long.toString(d.getTime())));
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be more transparent if all arguments for tryParseDate in this file were string literals to make it obvious what kinds of inputs tryParseDate accepts.

}

public void testTryParseDateIso8601() {
SimpleDateFormat fmt = new SimpleDateFormat(ISO8601_PATTERN, Locale.getDefault());
Date d = new Date(1234567890123L);
assertEquals(d, StringQuoter.tryParseDate(fmt.format(d)));
}

public void testTryParseDateZuluSuffix() throws Exception {
SimpleDateFormat fmt = new SimpleDateFormat(ISO8601_PATTERN, Locale.getDefault());
Date expected = fmt.parse("2024-01-15T10:30:00.000+0000");
assertEquals(expected, StringQuoter.tryParseDate("2024-01-15T10:30:00.000Z"));
}

public void testTryParseDateRfc2822() {
// RFC 2822 has second resolution.
SimpleDateFormat fmt = new SimpleDateFormat(RFC2822_PATTERN, Locale.getDefault());
Date d = new Date(1234567890000L);
assertEquals(d, StringQuoter.tryParseDate(fmt.format(d)));
}

public void testTryParseDateUnparseable() {
assertNull(StringQuoter.tryParseDate("not a date"));
}

/**
* SimpleDateFormat is not thread-safe; sharing a single instance across
* request threads on the server produced either ParseExceptions (returned as
* null) or silently wrong Dates. Hammer tryParseDate from many threads and
* make sure every call returns the expected value.
*/
public void testTryParseDateConcurrent() throws Exception {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is quite a bit going on here:

  1. pool.submit() does return a Future and that Future is not recorded and checked for exceptions. Without the ThreadLocal code in StringQuoter the SimpleDateFormat.parse() method can actually throw exceptions other than ParseException (for example NumberFormatException) during concurrent execution. Thus StringQuoter.tryParseDate() might throw exceptions which are only recorded in the Future. In that case the test might be green because neither null or wrong have been increased.
  2. The CountDownLatch.countDown() method is called directly after the submit for-loop. This does not guarantee that all 16 threads are actually already waiting. Sometimes they do, sometimes they don't. Submitted work can be put into a queue. What you really want is calling start.countDown() followed by start.await() inside the run() method and use a CountDownLatch(threads). Otherwise a second CountDownLatch is needed if we really want a dedicated signal to start the work in the test method itself.
  3. InterruptedException should be rethrown as a runtime exception. If 1. is implemented the test can check for 16 InterruptedException in the Future to figure out if the test has actually been executed or not. The test is generally brittle and we might not want to trust a green test if too many (or all threads) have been interrupted before actually doing anything.
  4. start.await() should probably use a timeout as well and some AssertionError with a message should be thrown if await returns via timeout. This is not strictly needed but gives a hint that thread synchronization did not happen in a timely manner. Also it kind of protects against a stupid future issue if for whatever reason CountDownLatch and ExecutorService have not been initialized with the same number (e.g. latch > pool threads).

SimpleDateFormat fmt = new SimpleDateFormat(ISO8601_PATTERN, Locale.getDefault());
final Date expected = new Date(1234567890123L);
final String input = fmt.format(expected);

final int threads = 16;
final int perThread = 2000;
final CountDownLatch start = new CountDownLatch(1);
final AtomicInteger nulls = new AtomicInteger();
final AtomicInteger wrong = new AtomicInteger();
ExecutorService pool = Executors.newFixedThreadPool(threads);
try {
for (int i = 0; i < threads; i++) {
pool.submit(new Runnable() {
@Override
public void run() {
try {
start.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
for (int j = 0; j < perThread; j++) {
Date r = StringQuoter.tryParseDate(input);
if (r == null) {
nulls.incrementAndGet();
} else if (!expected.equals(r)) {
wrong.incrementAndGet();
}
}
}
});
}
start.countDown();
pool.shutdown();
assertTrue("threads did not finish in time", pool.awaitTermination(60, TimeUnit.SECONDS));
} finally {
pool.shutdownNow();
}
assertEquals("parses returned null", 0, nulls.get());
assertEquals("parses returned wrong date", 0, wrong.get());
}
}