1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
|
/*
* Copyright (C) 2006 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.inject;
import static com.google.inject.Asserts.assertContains;
import static com.google.inject.name.Names.named;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import com.google.common.base.Joiner;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Maps;
import com.google.inject.matcher.Matchers;
import com.google.inject.name.Named;
import com.google.inject.spi.DefaultBindingScopingVisitor;
import com.google.inject.spi.Element;
import com.google.inject.spi.Elements;
import com.google.inject.spi.Message;
import com.google.inject.spi.PrivateElements;
import com.google.inject.spi.ProvisionListener;
import com.google.inject.util.Providers;
import java.io.IOException;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import junit.framework.TestCase;
/** @author crazybob@google.com (Bob Lee) */
public class ScopesTest extends TestCase {
static final long DEADLOCK_TIMEOUT_SECONDS = 1;
private final AbstractModule singletonsModule =
new AbstractModule() {
@Override
protected void configure() {
bind(BoundAsSingleton.class).in(Scopes.SINGLETON);
bind(AnnotatedSingleton.class);
bind(EagerSingleton.class).asEagerSingleton();
bind(LinkedSingleton.class).to(RealLinkedSingleton.class);
bind(DependsOnJustInTimeSingleton.class);
bind(NotASingleton.class);
bind(ImplementedBySingleton.class).in(Scopes.SINGLETON);
bind(ProvidedBySingleton.class).in(Scopes.SINGLETON);
}
};
@Override
protected void setUp() throws Exception {
AnnotatedSingleton.nextInstanceId = 0;
BoundAsSingleton.nextInstanceId = 0;
EagerSingleton.nextInstanceId = 0;
RealLinkedSingleton.nextInstanceId = 0;
JustInTimeSingleton.nextInstanceId = 0;
NotASingleton.nextInstanceId = 0;
Implementation.nextInstanceId = 0;
ProvidedBySingleton.nextInstanceId = 0;
ThrowingSingleton.nextInstanceId = 0;
}
public void testSingletons() {
Injector injector = Guice.createInjector(singletonsModule);
assertSame(
injector.getInstance(BoundAsSingleton.class), injector.getInstance(BoundAsSingleton.class));
assertSame(
injector.getInstance(AnnotatedSingleton.class),
injector.getInstance(AnnotatedSingleton.class));
assertSame(
injector.getInstance(EagerSingleton.class), injector.getInstance(EagerSingleton.class));
assertSame(
injector.getInstance(LinkedSingleton.class), injector.getInstance(LinkedSingleton.class));
assertSame(
injector.getInstance(JustInTimeSingleton.class),
injector.getInstance(JustInTimeSingleton.class));
assertNotSame(
injector.getInstance(NotASingleton.class), injector.getInstance(NotASingleton.class));
assertSame(
injector.getInstance(ImplementedBySingleton.class),
injector.getInstance(ImplementedBySingleton.class));
assertSame(
injector.getInstance(ProvidedBySingleton.class),
injector.getInstance(ProvidedBySingleton.class));
}
public void testJustInTimeAnnotatedSingleton() {
Injector injector = Guice.createInjector();
assertSame(
injector.getInstance(AnnotatedSingleton.class),
injector.getInstance(AnnotatedSingleton.class));
}
public void testSingletonIsPerInjector() {
assertNotSame(
Guice.createInjector().getInstance(AnnotatedSingleton.class),
Guice.createInjector().getInstance(AnnotatedSingleton.class));
}
public void testOverriddingAnnotation() {
Injector injector =
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bind(AnnotatedSingleton.class).in(Scopes.NO_SCOPE);
}
});
assertNotSame(
injector.getInstance(AnnotatedSingleton.class),
injector.getInstance(AnnotatedSingleton.class));
}
public void testScopingAnnotationsOnAbstractTypeViaBind() {
try {
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bind(A.class).to(AImpl.class);
}
});
fail();
} catch (CreationException expected) {
assertContains(
expected.getMessage(),
"ScopesTest$A is annotated with Singleton, but scope annotations are not supported for"
+ " abstract types.",
"at ScopesTest$A.class");
}
}
@SuppressWarnings("InjectScopeAnnotationOnInterfaceOrAbstractClass") // for testing
@Singleton
interface A {}
static class AImpl implements A {}
@Retention(RUNTIME)
@interface Component {}
@SuppressWarnings("InjectScopeAnnotationOnInterfaceOrAbstractClass") // for testing
@Component
@Singleton
interface ComponentAnnotationTest {}
static class ComponentAnnotationTestImpl implements ComponentAnnotationTest {}
public void testScopingAnnotationsOnAbstractTypeIsValidForComponent() {
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bind(ComponentAnnotationTest.class).to(ComponentAnnotationTestImpl.class);
}
});
}
public void testScopingAnnotationsOnAbstractTypeViaImplementedBy() {
try {
Guice.createInjector().getInstance(D.class);
fail();
} catch (ConfigurationException expected) {
assertContains(
expected.getMessage(),
"ScopesTest$D is annotated with Singleton, but scope annotations are not supported for"
+ " abstract types.",
"at ScopesTest$D.class");
}
}
@SuppressWarnings("InjectScopeAnnotationOnInterfaceOrAbstractClass") // for testing
@Singleton
@ImplementedBy(DImpl.class)
interface D {}
static class DImpl implements D {}
public void testScopingAnnotationsOnAbstractTypeViaProvidedBy() {
try {
Guice.createInjector().getInstance(E.class);
fail();
} catch (ConfigurationException expected) {
assertContains(
expected.getMessage(),
"ScopesTest$E is annotated with Singleton, but scope annotations are not supported for"
+ " abstract types.",
"at ScopesTest$E.class");
}
}
@SuppressWarnings("InjectScopeAnnotationOnInterfaceOrAbstractClass") // for testing
@Singleton
@ProvidedBy(EProvider.class)
interface E {}
static class EProvider implements Provider<E> {
@Override
public E get() {
return null;
}
}
public void testScopeUsedButNotBound() {
try {
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bind(B.class).in(CustomScoped.class);
bind(C.class);
}
});
fail();
} catch (CreationException expected) {
assertContains(
expected.getMessage(),
"No scope is bound to ScopesTest$CustomScoped.",
"1 : ScopesTest$5.configure",
"2 : ScopesTest$C.class");
}
}
static class B {}
@CustomScoped
static class C {}
public void testSingletonsInProductionStage() {
Guice.createInjector(Stage.PRODUCTION, singletonsModule);
assertEquals(1, AnnotatedSingleton.nextInstanceId);
assertEquals(1, BoundAsSingleton.nextInstanceId);
assertEquals(1, EagerSingleton.nextInstanceId);
assertEquals(1, RealLinkedSingleton.nextInstanceId);
assertEquals(1, JustInTimeSingleton.nextInstanceId);
assertEquals(0, NotASingleton.nextInstanceId);
}
public void testSingletonsInDevelopmentStage() {
Guice.createInjector(Stage.DEVELOPMENT, singletonsModule);
assertEquals(0, AnnotatedSingleton.nextInstanceId);
assertEquals(0, BoundAsSingleton.nextInstanceId);
assertEquals(1, EagerSingleton.nextInstanceId);
assertEquals(0, RealLinkedSingleton.nextInstanceId);
assertEquals(0, JustInTimeSingleton.nextInstanceId);
assertEquals(0, NotASingleton.nextInstanceId);
}
public void testSingletonScopeIsNotSerializable() throws IOException {
Asserts.assertNotSerializable(Scopes.SINGLETON);
}
public void testNoScopeIsNotSerializable() throws IOException {
Asserts.assertNotSerializable(Scopes.NO_SCOPE);
}
public void testUnscopedProviderWorksOutsideOfRequestedScope() {
final RememberProviderScope scope = new RememberProviderScope();
Injector injector =
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bindScope(CustomScoped.class, scope);
bind(List.class).to(ArrayList.class).in(CustomScoped.class);
}
});
injector.getInstance(List.class);
Provider<?> listProvider = scope.providers.get(Key.get(List.class));
// this line fails with a NullPointerException because the Providers
// passed to Scope.scope() don't work outside of the scope() method.
assertTrue(listProvider.get() instanceof ArrayList);
}
static class OuterRuntimeModule extends AbstractModule {
@Override
protected void configure() {
install(new InnerRuntimeModule());
}
}
static class InnerRuntimeModule extends AbstractModule {
@Override
protected void configure() {
bindScope(NotRuntimeRetainedScoped.class, Scopes.NO_SCOPE);
}
}
public void testScopeAnnotationWithoutRuntimeRetention() {
try {
Guice.createInjector(new OuterRuntimeModule());
fail();
} catch (CreationException expected) {
assertContains(
expected.getMessage(),
"Please annotate ScopesTest$NotRuntimeRetainedScoped with @Retention(RUNTIME).",
"at ScopesTest$InnerRuntimeModule.configure",
"ScopesTest$OuterRuntimeModule -> ScopesTest$InnerRuntimeModule");
}
}
static class OuterDeprecatedModule extends AbstractModule {
@Override
protected void configure() {
install(new InnerDeprecatedModule());
}
}
static class InnerDeprecatedModule extends AbstractModule {
@Override
protected void configure() {
bindScope(Deprecated.class, Scopes.NO_SCOPE);
}
}
public void testBindScopeToAnnotationWithoutScopeAnnotation() {
try {
Guice.createInjector(new OuterDeprecatedModule());
fail();
} catch (CreationException expected) {
assertContains(
expected.getMessage(),
"Please annotate Deprecated with @ScopeAnnotation.",
"at ScopesTest$InnerDeprecatedModule.configure",
"installed by: ScopesTest$OuterDeprecatedModule -> ScopesTest$InnerDeprecatedModule");
}
}
static class OuterScopeModule extends AbstractModule {
@Override
protected void configure() {
install(new CustomNoScopeModule());
install(new CustomSingletonModule());
}
}
static class CustomNoScopeModule extends AbstractModule {
@Override
protected void configure() {
bindScope(CustomScoped.class, Scopes.NO_SCOPE);
}
}
static class CustomSingletonModule extends AbstractModule {
@Override
protected void configure() {
bindScope(CustomScoped.class, Scopes.SINGLETON);
}
}
public void testBindScopeTooManyTimes() {
try {
Guice.createInjector(new OuterScopeModule());
fail();
} catch (CreationException expected) {
assertContains(
expected.getMessage(),
"Scope Scopes.NO_SCOPE is already bound to ScopesTest$CustomScoped",
"ScopesTest$OuterScopeModule -> ScopesTest$CustomNoScopeModule",
"Cannot bind Scopes.SINGLETON.",
"at ScopesTest$CustomSingletonModule.configure",
"ScopesTest$OuterScopeModule -> ScopesTest$CustomSingletonModule");
}
}
public void testBindDuplicateScope() {
Injector injector =
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bindScope(CustomScoped.class, Scopes.SINGLETON);
bindScope(CustomScoped.class, Scopes.SINGLETON);
}
});
assertSame(
injector.getInstance(AnnotatedCustomScoped.class),
injector.getInstance(AnnotatedCustomScoped.class));
}
public void testDuplicateScopeAnnotations() {
Injector injector =
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bindScope(CustomScoped.class, Scopes.NO_SCOPE);
}
});
try {
injector.getInstance(SingletonAndCustomScoped.class);
fail();
} catch (ConfigurationException expected) {
assertContains(
expected.getMessage(),
"More than one scope annotation was found: Singleton and ScopesTest$CustomScoped.",
"while locating ScopesTest$SingletonAndCustomScoped");
}
}
public void testNullScopedAsASingleton() {
Injector injector =
Guice.createInjector(
new AbstractModule() {
final Iterator<String> values = Arrays.asList(null, "A").iterator();
@Provides
@Singleton
String provideString() {
return values.next();
}
});
assertNull(injector.getInstance(String.class));
assertNull(injector.getInstance(String.class));
assertNull(injector.getInstance(String.class));
}
class RememberProviderScope implements Scope {
final Map<Key<?>, Provider<?>> providers = Maps.newHashMap();
@Override
public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
providers.put(key, unscoped);
return unscoped;
}
}
public void testSingletonAnnotationOnParameterizedType() {
Injector injector = Guice.createInjector();
assertSame(
injector.getInstance(new Key<Injected<String>>() {}),
injector.getInstance(new Key<Injected<String>>() {}));
assertSame(
injector.getInstance(new Key<In<Integer>>() {}),
injector.getInstance(new Key<In<Short>>() {}));
}
@ImplementedBy(Injected.class)
public interface In<T> {}
@Singleton
public static class Injected<T> implements In<T> {}
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RUNTIME)
@ScopeAnnotation
public @interface CustomScoped {}
static final Scope CUSTOM_SCOPE =
new Scope() {
@Override
public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
return Scopes.SINGLETON.scope(key, unscoped);
}
};
@SuppressWarnings("InjectScopeOrQualifierAnnotationRetention") // to check failure mode
@Target({ElementType.TYPE, ElementType.METHOD})
@ScopeAnnotation
public @interface NotRuntimeRetainedScoped {}
@CustomScoped
static class AnnotatedCustomScoped {}
@Singleton
static class AnnotatedSingleton {
static int nextInstanceId;
final int instanceId = nextInstanceId++;
}
static class BoundAsSingleton {
static int nextInstanceId;
final int instanceId = nextInstanceId++;
}
static class EagerSingleton {
static int nextInstanceId;
final int instanceId = nextInstanceId++;
}
interface LinkedSingleton {}
@Singleton
static class RealLinkedSingleton implements LinkedSingleton {
static int nextInstanceId;
final int instanceId = nextInstanceId++;
}
static class DependsOnJustInTimeSingleton {
@Inject JustInTimeSingleton justInTimeSingleton;
}
@Singleton
static class JustInTimeSingleton {
static int nextInstanceId;
final int instanceId = nextInstanceId++;
}
static class NotASingleton {
static int nextInstanceId;
final int instanceId = nextInstanceId++;
}
// suppress compiler error for testing
@SuppressWarnings({"MoreThanOneScopeAnnotationOnClass", "multiple-scope"})
@Singleton
@CustomScoped
static class SingletonAndCustomScoped {}
@ImplementedBy(Implementation.class)
static interface ImplementedBySingleton {}
@ProvidedBy(ImplementationProvider.class)
static class ProvidedBySingleton {
static int nextInstanceId;
final int instanceId = nextInstanceId++;
}
static class Implementation implements ImplementedBySingleton {
static int nextInstanceId;
final int instanceId = nextInstanceId++;
}
static class ImplementationProvider implements Provider<ProvidedBySingleton> {
@Override
public ProvidedBySingleton get() {
return new ProvidedBySingleton();
}
}
public void testScopeThatGetsAnUnrelatedObject() {
Injector injector =
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bind(B.class);
bind(C.class);
ProviderGetScope providerGetScope = new ProviderGetScope();
requestInjection(providerGetScope);
bindScope(CustomScoped.class, providerGetScope);
}
});
injector.getInstance(C.class);
}
class ProviderGetScope implements Scope {
@Inject Provider<B> bProvider;
@Override
public <T> Provider<T> scope(Key<T> key, final Provider<T> unscoped) {
return new Provider<T>() {
@Override
public T get() {
bProvider.get();
return unscoped.get();
}
};
}
}
public void testIsSingletonPositive() {
final Key<String> a = Key.get(String.class, named("A"));
final Key<String> b = Key.get(String.class, named("B"));
final Key<String> c = Key.get(String.class, named("C"));
final Key<String> d = Key.get(String.class, named("D"));
final Key<String> e = Key.get(String.class, named("E"));
final Key<String> f = Key.get(String.class, named("F"));
final Key<String> g = Key.get(String.class, named("G"));
final Key<Object> h = Key.get(Object.class, named("H"));
final Key<String> i = Key.get(String.class, named("I"));
Module singletonBindings =
new AbstractModule() {
@Override
protected void configure() {
bind(a).to(b);
bind(b).to(c);
bind(c).toProvider(Providers.of("c")).in(Scopes.SINGLETON);
bind(d).toInstance("d");
bind(e).toProvider(Providers.of("e")).asEagerSingleton();
bind(f).toProvider(Providers.of("f")).in(Singleton.class);
bind(h).to(AnnotatedSingleton.class);
install(
new PrivateModule() {
@Override
protected void configure() {
bind(i).toProvider(Providers.of("i")).in(Singleton.class);
expose(i);
}
});
}
@Provides
@Named("G")
@Singleton
String provideG() {
return "g";
}
};
@SuppressWarnings("unchecked") // we know the module contains only bindings
List<Element> moduleBindings = Elements.getElements(singletonBindings);
ImmutableMap<Key<?>, Binding<?>> map = indexBindings(moduleBindings);
assertFalse(Scopes.isSingleton(map.get(a))); // linked bindings are not followed by modules
assertFalse(Scopes.isSingleton(map.get(b)));
assertTrue(Scopes.isSingleton(map.get(c)));
assertTrue(Scopes.isSingleton(map.get(d)));
assertTrue(Scopes.isSingleton(map.get(e)));
assertTrue(Scopes.isSingleton(map.get(f)));
assertTrue(Scopes.isSingleton(map.get(g)));
assertFalse(Scopes.isSingleton(map.get(h))); // annotated classes are not followed by modules
assertTrue(Scopes.isSingleton(map.get(i)));
Injector injector = Guice.createInjector(singletonBindings);
assertTrue(Scopes.isSingleton(injector.getBinding(a)));
assertTrue(Scopes.isSingleton(injector.getBinding(b)));
assertTrue(Scopes.isSingleton(injector.getBinding(c)));
assertTrue(Scopes.isSingleton(injector.getBinding(d)));
assertTrue(Scopes.isSingleton(injector.getBinding(e)));
assertTrue(Scopes.isSingleton(injector.getBinding(f)));
assertTrue(Scopes.isSingleton(injector.getBinding(g)));
assertTrue(Scopes.isSingleton(injector.getBinding(h)));
assertTrue(Scopes.isSingleton(injector.getBinding(i)));
}
public void testIsSingletonNegative() {
final Key<String> a = Key.get(String.class, named("A"));
final Key<String> b = Key.get(String.class, named("B"));
final Key<String> c = Key.get(String.class, named("C"));
final Key<String> d = Key.get(String.class, named("D"));
final Key<String> e = Key.get(String.class, named("E"));
final Key<String> f = Key.get(String.class, named("F"));
Module singletonBindings =
new AbstractModule() {
@Override
protected void configure() {
bind(a).to(b);
bind(b).to(c);
bind(c).toProvider(Providers.of("c")).in(Scopes.NO_SCOPE);
bind(d).toProvider(Providers.of("d")).in(CustomScoped.class);
bindScope(CustomScoped.class, Scopes.NO_SCOPE);
install(
new PrivateModule() {
@Override
protected void configure() {
bind(f).toProvider(Providers.of("f")).in(CustomScoped.class);
expose(f);
}
});
}
@Provides
@Named("E")
@CustomScoped
String provideE() {
return "e";
}
};
@SuppressWarnings("unchecked") // we know the module contains only bindings
List<Element> moduleBindings = Elements.getElements(singletonBindings);
ImmutableMap<Key<?>, Binding<?>> map = indexBindings(moduleBindings);
assertFalse(Scopes.isSingleton(map.get(a)));
assertFalse(Scopes.isSingleton(map.get(b)));
assertFalse(Scopes.isSingleton(map.get(c)));
assertFalse(Scopes.isSingleton(map.get(d)));
assertFalse(Scopes.isSingleton(map.get(e)));
assertFalse(Scopes.isSingleton(map.get(f)));
Injector injector = Guice.createInjector(singletonBindings);
assertFalse(Scopes.isSingleton(injector.getBinding(a)));
assertFalse(Scopes.isSingleton(injector.getBinding(b)));
assertFalse(Scopes.isSingleton(injector.getBinding(c)));
assertFalse(Scopes.isSingleton(injector.getBinding(d)));
assertFalse(Scopes.isSingleton(injector.getBinding(e)));
assertFalse(Scopes.isSingleton(injector.getBinding(f)));
}
public void testIsScopedPositive() {
final Key<String> a = Key.get(String.class, named("A"));
final Key<String> b = Key.get(String.class, named("B"));
final Key<String> c = Key.get(String.class, named("C"));
final Key<String> d = Key.get(String.class, named("D"));
final Key<String> e = Key.get(String.class, named("E"));
final Key<Object> f = Key.get(Object.class, named("F"));
final Key<String> g = Key.get(String.class, named("G"));
Module customBindings =
new AbstractModule() {
@Override
protected void configure() {
bindScope(CustomScoped.class, CUSTOM_SCOPE);
bind(a).to(b);
bind(b).to(c);
bind(c).toProvider(Providers.of("c")).in(CUSTOM_SCOPE);
bind(d).toProvider(Providers.of("d")).in(CustomScoped.class);
bind(f).to(AnnotatedCustomScoped.class);
install(
new PrivateModule() {
@Override
protected void configure() {
bind(g).toProvider(Providers.of("g")).in(CustomScoped.class);
expose(g);
}
});
}
@Provides
@Named("E")
@CustomScoped
String provideE() {
return "e";
}
};
@SuppressWarnings("unchecked") // we know the module contains only bindings
List<Element> moduleBindings = Elements.getElements(customBindings);
ImmutableMap<Key<?>, Binding<?>> map = indexBindings(moduleBindings);
assertFalse(isCustomScoped(map.get(a))); // linked bindings are not followed by modules
assertFalse(isCustomScoped(map.get(b)));
assertTrue(isCustomScoped(map.get(c)));
assertTrue(isCustomScoped(map.get(d)));
assertTrue(isCustomScoped(map.get(e)));
assertFalse(isCustomScoped(map.get(f))); // annotated classes are not followed by modules
assertTrue(isCustomScoped(map.get(g)));
Injector injector = Guice.createInjector(customBindings);
assertTrue(isCustomScoped(injector.getBinding(a)));
assertTrue(isCustomScoped(injector.getBinding(b)));
assertTrue(isCustomScoped(injector.getBinding(c)));
assertTrue(isCustomScoped(injector.getBinding(d)));
assertTrue(isCustomScoped(injector.getBinding(e)));
assertTrue(isCustomScoped(injector.getBinding(f)));
assertTrue(isCustomScoped(injector.getBinding(g)));
}
public void testIsScopedNegative() {
final Key<String> a = Key.get(String.class, named("A"));
final Key<String> b = Key.get(String.class, named("B"));
final Key<String> c = Key.get(String.class, named("C"));
final Key<String> d = Key.get(String.class, named("D"));
final Key<String> e = Key.get(String.class, named("E"));
final Key<String> f = Key.get(String.class, named("F"));
final Key<String> g = Key.get(String.class, named("G"));
final Key<String> h = Key.get(String.class, named("H"));
Module customBindings =
new AbstractModule() {
@Override
protected void configure() {
bind(a).to(b);
bind(b).to(c);
bind(c).toProvider(Providers.of("c")).in(Scopes.NO_SCOPE);
bind(d).toProvider(Providers.of("d")).in(Singleton.class);
install(
new PrivateModule() {
@Override
protected void configure() {
bind(f).toProvider(Providers.of("f")).in(Singleton.class);
expose(f);
}
});
bind(g).toInstance("g");
bind(h).toProvider(Providers.of("h")).asEagerSingleton();
}
@Provides
@Named("E")
@Singleton
String provideE() {
return "e";
}
};
@SuppressWarnings("unchecked") // we know the module contains only bindings
List<Element> moduleBindings = Elements.getElements(customBindings);
ImmutableMap<Key<?>, Binding<?>> map = indexBindings(moduleBindings);
assertFalse(isCustomScoped(map.get(a)));
assertFalse(isCustomScoped(map.get(b)));
assertFalse(isCustomScoped(map.get(c)));
assertFalse(isCustomScoped(map.get(d)));
assertFalse(isCustomScoped(map.get(e)));
assertFalse(isCustomScoped(map.get(f)));
assertFalse(isCustomScoped(map.get(g)));
assertFalse(isCustomScoped(map.get(h)));
Injector injector = Guice.createInjector(customBindings);
assertFalse(isCustomScoped(injector.getBinding(a)));
assertFalse(isCustomScoped(injector.getBinding(b)));
assertFalse(isCustomScoped(injector.getBinding(c)));
assertFalse(isCustomScoped(injector.getBinding(d)));
assertFalse(isCustomScoped(injector.getBinding(e)));
assertFalse(isCustomScoped(injector.getBinding(f)));
assertFalse(isCustomScoped(injector.getBinding(g)));
assertFalse(isCustomScoped(injector.getBinding(h)));
}
private boolean isCustomScoped(Binding<?> binding) {
return Scopes.isScoped(binding, CUSTOM_SCOPE, CustomScoped.class);
}
ImmutableMap<Key<?>, Binding<?>> indexBindings(Iterable<Element> elements) {
ImmutableMap.Builder<Key<?>, Binding<?>> builder = ImmutableMap.builder();
for (Element element : elements) {
if (element instanceof Binding) {
Binding<?> binding = (Binding<?>) element;
builder.put(binding.getKey(), binding);
} else if (element instanceof PrivateElements) {
PrivateElements privateElements = (PrivateElements) element;
Map<Key<?>, Binding<?>> privateBindings = indexBindings(privateElements.getElements());
for (Key<?> exposed : privateElements.getExposedKeys()) {
builder.put(exposed, privateBindings.get(exposed));
}
}
}
return builder.build();
}
@Singleton
static class ThrowingSingleton {
static int nextInstanceId;
final int instanceId = nextInstanceId++;
ThrowingSingleton() {
if (instanceId == 0) {
throw new RuntimeException();
}
}
}
public void testSingletonConstructorThrows() {
Injector injector = Guice.createInjector();
try {
injector.getInstance(ThrowingSingleton.class);
fail();
} catch (ProvisionException expected) {
}
// this behaviour is unspecified. If we change Guice to re-throw the exception, this test
// should be changed
injector.getInstance(ThrowingSingleton.class);
assertEquals(2, ThrowingSingleton.nextInstanceId);
}
/**
* Should only be created by {@link SBarrierProvider}.
*
* <p>{@code S} stands for synchronization.
*
* @see SBarrierProvider
*/
static class S {
private S(int preventInjectionWithoutProvider) {}
}
/**
* Provides all the instances of S simultaneously using {@link CyclicBarrier} with {@code
* nThreads}. Intended to be used for threads synchronization during injection.
*/
static class SBarrierProvider implements Provider<S> {
final CyclicBarrier barrier;
volatile boolean barrierPassed = false;
SBarrierProvider(int nThreads) {
barrier = new CyclicBarrier(nThreads, () -> barrierPassed = true);
}
@Override
public S get() {
try {
if (!barrierPassed) {
// only if we're triggering barrier for the first time
barrier.await(DEADLOCK_TIMEOUT_SECONDS, TimeUnit.SECONDS);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return new S(0);
}
}
/**
* Tests that different injectors should not affect each other.
*
* <p>This creates a second thread to work in parallel, to create two instances of {@link S} as
* the same time. If the lock if not granular enough (i.e. JVM-wide) then they would block each
* other creating a deadlock and await timeout.
*/
public void testInjectorsDontDeadlockOnSingletons() throws Exception {
final Provider<S> provider = new SBarrierProvider(2);
final Injector injector =
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
Thread.currentThread().setName("S.class[1]");
bind(S.class).toProvider(provider).in(Scopes.SINGLETON);
}
});
final Injector secondInjector =
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
Thread.currentThread().setName("S.class[2]");
bind(S.class).toProvider(provider).in(Scopes.SINGLETON);
}
});
Future<S> secondThreadResult =
Executors.newSingleThreadExecutor().submit(() -> secondInjector.getInstance(S.class));
S firstS = injector.getInstance(S.class);
S secondS = secondThreadResult.get();
assertNotSame(firstS, secondS);
}
@ImplementedBy(GImpl.class)
interface G {}
@Singleton
static class GImpl implements G {
final H h;
/** Relies on Guice implementation to inject S first and H later, which provides a barrier . */
@Inject
GImpl(S synchronizationBarrier, H h) {
this.h = h;
}
}
@ImplementedBy(HImpl.class)
interface H {}
@Singleton
static class HImpl implements H {
final G g;
/** Relies on Guice implementation to inject S first and G later, which provides a barrier . */
@Inject
HImpl(S synchronizationBarrier, G g) throws Exception {
this.g = g;
}
}
/**
* Tests that injector can create two singletons with circular dependency in parallel.
*
* <p>This creates two threads to work in parallel, to create instances of {@link G} and {@link
* H}. Creation is synchronized by injection of {@link S}, first thread would block until second
* would be inside a singleton creation as well.
*
* <p>Both instances are created by sibling injectors, that share singleton scope. Verifies that
* exactly one circular proxy object is created.
*/
public void testSiblingInjectorGettingCircularSingletonsOneCircularProxy() throws Exception {
final Provider<S> provider = new SBarrierProvider(2);
final Injector injector =
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bind(S.class).toProvider(provider);
}
});
Future<G> firstThreadResult =
Executors.newSingleThreadExecutor()
.submit(
() -> {
Thread.currentThread().setName("G.class");
return injector.createChildInjector().getInstance(G.class);
});
Future<H> secondThreadResult =
Executors.newSingleThreadExecutor()
.submit(
() -> {
Thread.currentThread().setName("H.class");
return injector.createChildInjector().getInstance(H.class);
});
// using separate threads to avoid potential deadlock on the main thread
// waiting twice as much to be sure that both would time out in their respective barriers
GImpl g = (GImpl) firstThreadResult.get(DEADLOCK_TIMEOUT_SECONDS * 3, TimeUnit.SECONDS);
HImpl h = (HImpl) secondThreadResult.get(DEADLOCK_TIMEOUT_SECONDS * 3, TimeUnit.SECONDS);
// Check that G and H created are not proxied
assertTrue(!Scopes.isCircularProxy(g) && !Scopes.isCircularProxy(h));
// Check that we have no more than one circular proxy created
assertFalse(Scopes.isCircularProxy(g.h) && Scopes.isCircularProxy(h.g));
// Check that non-proxy variable points to another singleton
assertTrue(g.h == h || h.g == g);
// Check correct proxy initialization as default equals implementation would
assertEquals(g.h, h);
assertEquals(h.g, g);
}
@Singleton
static class I0 {
/** Relies on Guice implementation to inject S first, which provides a barrier . */
@Inject
I0(I1 i) {}
}
@Singleton
static class I1 {
/** Relies on Guice implementation to inject S first, which provides a barrier . */
@Inject
I1(S synchronizationBarrier, I2 i) {}
}
@Singleton
static class I2 {
/** Relies on Guice implementation to inject S first, which provides a barrier . */
@Inject
I2(J1 j) {}
}
@Singleton
static class J0 {
/** Relies on Guice implementation to inject S first, which provides a barrier . */
@Inject
J0(J1 j) {}
}
@Singleton
static class J1 {
/** Relies on Guice implementation to inject S first, which provides a barrier . */
@Inject
J1(S synchronizationBarrier, J2 j) {}
}
@Singleton
static class J2 {
/** Relies on Guice implementation to inject S first, which provides a barrier . */
@Inject
J2(K1 k) {}
}
@Singleton
static class K0 {
/** Relies on Guice implementation to inject S first, which provides a barrier . */
@Inject
K0(K1 k) {}
}
@Singleton
static class K1 {
/** Relies on Guice implementation to inject S first, which provides a barrier . */
@Inject
K1(S synchronizationBarrier, K2 k) {}
}
@Singleton
static class K2 {
/** Relies on Guice implementation to inject S first, which provides a barrier . */
@Inject
K2(I1 i) {}
}
/**
* Check that circular dependencies on non-interfaces are correctly resolved in multi-threaded
* case. And that an error message constructed is a good one.
*
* <p>I0 -> I1 -> I2 -> J1 and J0 -> J1 -> J2 -> K1 and K0 -> K1 -> K2, where I1, J1 and K1 are
* created in parallel.
*
* <p>Creation is synchronized by injection of {@link S}, first thread would block until second
* would be inside a singleton creation as well.
*
* <p>Verifies that provision results in an error, that spans two threads and has a dependency
* cycle.
*/
public void testUnresolvableSingletonCircularDependencyErrorMessage() throws Exception {
final Provider<S> provider = new SBarrierProvider(3);
final Injector injector =
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bind(S.class).toProvider(provider);
}
});
FutureTask<I0> firstThreadResult = new FutureTask<>(() -> injector.getInstance(I0.class));
Thread i0Thread = new Thread(firstThreadResult, "I0.class");
// we need to call toString() now, because the toString() changes after the thread exits.
String i0ThreadString = i0Thread.toString();
i0Thread.start();
FutureTask<J0> secondThreadResult = new FutureTask<>(() -> injector.getInstance(J0.class));
Thread j0Thread = new Thread(secondThreadResult, "J0.class");
String j0ThreadString = j0Thread.toString();
j0Thread.start();
FutureTask<K0> thirdThreadResult = new FutureTask<>(() -> injector.getInstance(K0.class));
Thread k0Thread = new Thread(thirdThreadResult, "K0.class");
String k0ThreadString = k0Thread.toString();
k0Thread.start();
// using separate threads to avoid potential deadlock on the main thread
// waiting twice as much to be sure that both would time out in their respective barriers
Throwable firstException = null;
Throwable secondException = null;
Throwable thirdException = null;
try {
firstThreadResult.get(DEADLOCK_TIMEOUT_SECONDS * 3, TimeUnit.SECONDS);
fail();
} catch (ExecutionException e) {
firstException = e.getCause();
}
try {
secondThreadResult.get(DEADLOCK_TIMEOUT_SECONDS * 3, TimeUnit.SECONDS);
fail();
} catch (ExecutionException e) {
secondException = e.getCause();
}
try {
thirdThreadResult.get(DEADLOCK_TIMEOUT_SECONDS * 3, TimeUnit.SECONDS);
fail();
} catch (ExecutionException e) {
thirdException = e.getCause();
}
// verification of error messages generated
List<Message> errors = new ArrayList<>();
errors.addAll(((ProvisionException) firstException).getErrorMessages());
errors.addAll(((ProvisionException) secondException).getErrorMessages());
errors.addAll(((ProvisionException) thirdException).getErrorMessages());
// We want to find the longest error reported for a cycle spanning multiple threads
Message spanningError = null;
for (Message error : errors) {
if (error.getMessage().contains("Encountered circular dependency spanning several threads")) {
if (spanningError == null
|| spanningError.getMessage().length() < error.getMessage().length()) {
spanningError = error;
}
}
}
if (spanningError == null) {
fail(
"Couldn't find multi thread circular dependency error: "
+ Joiner.on("\n\n").join(errors));
}
String errorMessage = spanningError.getMessage();
assertContains(
errorMessage,
"Encountered circular dependency spanning several threads. Tried proxying "
+ this.getClass().getName());
assertFalse(
"Both I0 and J0 can not be a part of a dependency cycle",
errorMessage.contains(I0.class.getName()) && errorMessage.contains(J0.class.getName()));
assertFalse(
"Both J0 and K0 can not be a part of a dependency cycle",
errorMessage.contains(J0.class.getName()) && errorMessage.contains(K0.class.getName()));
assertFalse(
"Both K0 and I0 can not be a part of a dependency cycle",
errorMessage.contains(K0.class.getName()) && errorMessage.contains(I0.class.getName()));
ListMultimap<String, String> threadToSingletons = ArrayListMultimap.create();
boolean inSingletonsList = false;
String currentThread = null;
for (String errorLine : errorMessage.split("\\n")) {
if (errorLine.startsWith("Thread[")) {
inSingletonsList = true;
currentThread =
errorLine.substring(
0, errorLine.indexOf(" is holding locks the following singletons in the cycle:"));
} else if (inSingletonsList) {
if (errorLine.startsWith("\tat ")) {
inSingletonsList = false;
} else {
threadToSingletons.put(currentThread, errorLine);
}
}
}
assertEquals("All threads should be in the cycle", 3, threadToSingletons.keySet().size());
// NOTE: J0,K0,I0 are not reported because their locks are not part of the cycle.
assertEquals(
threadToSingletons.get(j0ThreadString),
ImmutableList.of(J1.class.getName(), J2.class.getName(), K1.class.getName()));
assertEquals(
threadToSingletons.get(k0ThreadString),
ImmutableList.of(K1.class.getName(), K2.class.getName(), I1.class.getName()));
assertEquals(
threadToSingletons.get(i0ThreadString),
ImmutableList.of(I1.class.getName(), I2.class.getName(), J1.class.getName()));
}
// Test for https://github.com/google/guice/issues/1032
public void testScopeAppliedByUserInsteadOfScoping() throws Exception {
Injector injector =
java.util.concurrent.Executors.newSingleThreadExecutor()
.submit(
() ->
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bindListener(Matchers.any(), new ScopeMutatingProvisionListener());
bind(SingletonClass.class);
}
}))
.get();
injector.getInstance(SingletonClass.class); // will fail here with NPE
}
@Singleton
static class SingletonClass {}
/** Uses Scope's public API to add a 'marker' into the provisioned instance's scope. */
private static final class ScopeMutatingProvisionListener implements ProvisionListener {
private static class ScopeMarker {
static final Provider<ScopeMarker> PROVIDER =
new Provider<ScopeMarker>() {
@Override
public ScopeMarker get() {
return new ScopeMarker();
}
};
}
@Override
public <T> void onProvision(final ProvisionInvocation<T> provisionInvocation) {
provisionInvocation.provision();
provisionInvocation
.getBinding()
.acceptScopingVisitor(
new DefaultBindingScopingVisitor<Void>() {
@Override
public Void visitScope(Scope scope) {
scope.scope(Key.get(ScopeMarker.class), ScopeMarker.PROVIDER);
return null;
}
});
}
}
public void testForInstanceOfNoScopingReturnsUnscoped() {
Injector injector =
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bind(AImpl.class).in(Scopes.NO_SCOPE);
}
});
assertTrue(
injector
.getBinding(Key.get(AImpl.class))
.acceptScopingVisitor(
new DefaultBindingScopingVisitor<Boolean>() {
@Override
protected Boolean visitOther() {
return false;
}
@Override
public Boolean visitNoScoping() {
return true;
}
}));
}
public void testScopedLinkedBindingDoesNotPropagateEagerSingleton() {
final Key<String> a = Key.get(String.class, named("A"));
final Key<String> b = Key.get(String.class, named("B"));
final Scope notInScopeScope =
new Scope() {
@Override
public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
return new Provider<T>() {
@Override
public T get() {
throw new IllegalStateException("Not in scope");
}
};
}
};
Module module =
new AbstractModule() {
@Override
protected void configure() {
bind(a).toInstance("a");
bind(b).to(a).in(CustomScoped.class);
bindScope(CustomScoped.class, notInScopeScope);
}
};
Injector injector = Guice.createInjector(module);
Provider<String> bProvider = injector.getProvider(b);
try {
bProvider.get();
fail("expected failure");
} catch (ProvisionException expected) {
}
}
}
|