-
Notifications
You must be signed in to change notification settings - Fork 389
Make StringQuoter date parsing thread-safe #10322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
niloc132
merged 4 commits into
gwtproject:main
from
metsw24-max:stringquoter-date-threadsafe
Jun 25, 2026
+134
−6
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
71b7f6e
Make StringQuoter date parsing thread-safe
metsw24-max 3e9386b
Add StringQuoter.tryParseDate tests, including a concurrency check
metsw24-max 9efec11
Use literal inputs and check worker futures in StringQuoter tests
metsw24-max dbf5570
Use GWT Project Authors copyright header in StringQuoterJreTest
metsw24-max File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
user/test/com/google/web/bindery/autobean/vm/StringQuoterJreTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()))); | ||
|
zbynek marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| 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 { | ||
|
jnehlmeier marked this conversation as resolved.
|
||
| 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()); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given it is a new file it should be
Copyright 2026 GWT Project Authors.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, header now reads Copyright 2026 GWT Project Authors. Pushed in dbf5570.