-
Notifications
You must be signed in to change notification settings - Fork 192
Fix collections on transaction query. #1571
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
mikereiche
merged 1 commit into
main
from
datacouch_1569_fix_collections_on_transaction_query
Oct 4, 2022
Merged
Changes from all commits
Commits
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
142 changes: 142 additions & 0 deletions
142
...ta/couchbase/transactions/CouchbaseTransactionalRepositoryCollectionIntegrationTests.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,142 @@ | ||
/* | ||
* Copyright 2022 the original author or authors | ||
* | ||
* 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 | ||
* | ||
* https://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 org.springframework.data.couchbase.transactions; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.springframework.data.couchbase.transactions.util.TransactionTestUtil.assertInTransaction; | ||
import static org.springframework.data.couchbase.transactions.util.TransactionTestUtil.assertNotInTransaction; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
import java.util.function.Consumer; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.couchbase.CouchbaseClientFactory; | ||
import org.springframework.data.couchbase.core.CouchbaseTemplate; | ||
import org.springframework.data.couchbase.domain.User; | ||
import org.springframework.data.couchbase.domain.UserRepository; | ||
import org.springframework.data.couchbase.transaction.error.TransactionSystemUnambiguousException; | ||
import org.springframework.data.couchbase.util.ClusterType; | ||
import org.springframework.data.couchbase.util.IgnoreWhen; | ||
import org.springframework.data.couchbase.util.JavaIntegrationTests; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
/** | ||
* Tests @Transactional with repository methods. | ||
* | ||
* @author Michael Reiche | ||
*/ | ||
@IgnoreWhen(clusterTypes = ClusterType.MOCKED) | ||
@SpringJUnitConfig( | ||
classes = { TransactionsConfig.class, CouchbaseTransactionalRepositoryIntegrationTests.UserService.class }) | ||
public class CouchbaseTransactionalRepositoryIntegrationTests extends JavaIntegrationTests { | ||
// intellij flags "Could not autowire" when config classes are specified with classes={...}. But they are populated. | ||
@Autowired UserRepository userRepo; | ||
@Autowired CouchbaseClientFactory couchbaseClientFactory; | ||
@Autowired UserService userService; | ||
@Autowired CouchbaseTemplate operations; | ||
|
||
@BeforeAll | ||
public static void beforeAll() { | ||
callSuperBeforeAll(new Object() {}); | ||
} | ||
|
||
@BeforeEach | ||
public void beforeEachTest() { | ||
assertNotInTransaction(); | ||
} | ||
|
||
@AfterEach | ||
public void afterEachTest() { | ||
assertNotInTransaction(); | ||
} | ||
|
||
@Test | ||
public void findByFirstname() { | ||
operations.insertById(User.class).one(new User(UUID.randomUUID().toString(), "Ada", "Lovelace")); | ||
|
||
List<User> users = userService.findByFirstname("Ada"); | ||
|
||
assertNotEquals(0, users.size()); | ||
} | ||
|
||
@Test | ||
public void save() { | ||
String id = UUID.randomUUID().toString(); | ||
|
||
userService.run(repo -> { | ||
assertInTransaction(); | ||
|
||
User user0 = repo.save(new User(id, "Ada", "Lovelace")); | ||
|
||
assertInTransaction(); | ||
|
||
// read your own write | ||
User user1 = operations.findById(User.class).one(id); | ||
assertNotNull(user1); | ||
|
||
assertInTransaction(); | ||
|
||
}); | ||
|
||
User user = operations.findById(User.class).one(id); | ||
assertNotNull(user); | ||
} | ||
|
||
@DisplayName("Test that repo.save() is actually performed transactionally, by forcing a rollback") | ||
@Test | ||
public void saveRolledBack() { | ||
String id = UUID.randomUUID().toString(); | ||
|
||
assertThrowsWithCause(() -> { | ||
userService.run(repo -> { | ||
User user = repo.save(new User(id, "Ada", "Lovelace")); | ||
SimulateFailureException.throwEx("fail"); | ||
}); | ||
}, TransactionSystemUnambiguousException.class, SimulateFailureException.class); | ||
|
||
User user = operations.findById(User.class).one(id); | ||
assertNull(user); | ||
} | ||
|
||
@Service // this will work in the unit tests even without @Service because of explicit loading by @SpringJUnitConfig | ||
static class UserService { | ||
@Autowired UserRepository userRepo; | ||
|
||
@Transactional | ||
public void run(Consumer<UserRepository> callback) { | ||
callback.accept(userRepo); | ||
} | ||
|
||
@Transactional | ||
public List<User> findByFirstname(String name) { | ||
return userRepo.findByFirstname(name); | ||
} | ||
|
||
} | ||
|
||
} |
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.
The TransactionAttemptContext.query overload that takes a Scope doesn't explicitly mark that as being @nullable (and in fact arguably should be checking it's not-null), so it would be less brittle if this code followed the specced API like so:
But it's ok. I'll just have to be careful to never change my interface to require scope to be non-null.