Skip to content

Commit 6fd37b8

Browse files
committed
Parameterice slow beans and rename required to optional
1 parent 6f7d1b5 commit 6fd37b8

File tree

21 files changed

+314
-123
lines changed

21 files changed

+314
-123
lines changed

src/integrationTest/groovy/annotated/SlowBeanCreationSpec.groovy

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,28 @@ class SlowBeanCreationSpec extends Specification {
1919
rootLogger.detachAppender(appender)
2020
}
2121

22-
def "should log slow bean initialization"() {
22+
def "should log slow bean creation"() {
2323
given:
24-
Context context = Context.scanPackage(annotated.samples.slow_beans.Bar)
24+
Context context = Context.scanPackage(annotated.samples.slow_beans.Foo)
2525
when:
26-
annotated.samples.slow_beans.Baz baz = context.get(annotated.samples.slow_beans.Baz)
26+
context.get(annotated.samples.slow_beans.Foo)
27+
28+
then:
29+
List<String> logs = appender.getLogsByMessagePrefix("Slow bean creation")
30+
logs.size() == 2
31+
logs.get(0).startsWith("[WARN] Slow bean creation. Bean: Bar")
32+
logs.get(1).startsWith("[WARN] Slow bean creation. Bean: Foo")
33+
}
34+
35+
def "should skip logging slow bean creation with custom threshold"() {
36+
given:
37+
Context context = Context.scanPackage(annotated.samples.slow_beans_annotated.Foo)
38+
when:
39+
context.get(annotated.samples.slow_beans_annotated.Foo)
2740

2841
then:
2942
List<String> logs = appender.getLogsByMessagePrefix("Slow bean creation")
3043
logs.size() == 1
31-
logs.first().startsWith("[WARN] Slow bean creation. Bean: Bar")
44+
logs.get(0).startsWith("[WARN] Slow bean creation. Bean: Bar")
3245
}
3346
}

src/integrationTest/groovy/annotated/samples/config/SampleConfig.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package annotated.samples.config
22

33
import com.coditory.quark.context.annotations.Bean
4-
import com.coditory.quark.context.annotations.Dependency
54
import com.coditory.quark.context.annotations.Configuration
5+
import com.coditory.quark.context.annotations.Dependency
66

77
@Configuration
88
class SampleConfig {
@@ -25,7 +25,7 @@ class SampleConfig {
2525
Foo foo2(
2626
Bar bar,
2727
@Dependency("foooo") Foo foooo,
28-
@Dependency(required = false) Baz baz
28+
@Dependency(optional = true) Baz baz
2929
) {
3030
return new Foo("foo2", bar)
3131
}

src/integrationTest/groovy/annotated/samples/named_bean/Bar.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Bar {
1010

1111
Bar(
1212
@Dependency(name = "fooo") Foo foo,
13-
@Dependency(name = "baaz", required = false) Baz baz
13+
@Dependency(name = "baaz", optional = true) Baz baz
1414
) {
1515
this.foo = foo
1616
this.baz = baz

src/integrationTest/groovy/annotated/samples/optional_bean/Bar.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Bar {
88
final Foo foo;
99
final Baz baz;
1010

11-
Bar(@Dependency(required = false) Foo foo, @Dependency(required = false) Baz baz) {
11+
Bar(@Dependency(optional = true) Foo foo, @Dependency(optional = true) Baz baz) {
1212
this.foo = foo
1313
this.baz = baz
1414
}

src/integrationTest/groovy/annotated/samples/optional_multiple_deps/Bar.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import com.coditory.quark.context.annotations.Dependency
77
class Bar {
88
final List<Foo> foo
99

10-
Bar(@Dependency(required = false) List<Foo> foo) {
10+
Bar(@Dependency(optional = true) List<Foo> foo) {
1111
this.foo = foo
1212
}
1313
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package annotated.samples.slow_beans_annotated
2+
3+
class Bar {
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package annotated.samples.slow_beans_annotated
2+
3+
class Baz {
4+
final annotated.samples.slow_beans_annotated.Bar bar
5+
6+
Baz(annotated.samples.slow_beans_annotated.Bar bar) {
7+
this.bar = bar
8+
}
9+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package annotated.samples.slow_beans_annotated
2+
3+
import com.coditory.quark.context.annotations.Bean
4+
import com.coditory.quark.context.annotations.Configuration
5+
6+
@Configuration
7+
class ConfigInit {
8+
@Bean(creationTimeMs = 100)
9+
Bar bar() {
10+
Thread.sleep(800)
11+
return new Bar()
12+
}
13+
14+
@Bean
15+
Baz baz(Bar bar) {
16+
return new Baz(bar)
17+
}
18+
19+
@Bean(creationTimeMs = 1100)
20+
Foo foo(Baz baz) {
21+
Thread.sleep(1_000)
22+
return new Foo(baz)
23+
}
24+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package annotated.samples.slow_beans_annotated
2+
3+
4+
import static java.util.Objects.requireNonNull
5+
6+
class Foo {
7+
final Baz baz
8+
9+
Foo(Baz baz) {
10+
this.baz = requireNonNull(baz)
11+
}
12+
13+
Foo(annotated.samples.slow_beans_annotated.Bar bar) {
14+
throw new IllegalStateException("This constructor should not be used")
15+
}
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.coditory.quark.context;
2+
3+
import com.coditory.quark.context.annotations.Bean;
4+
import com.coditory.quark.context.annotations.Configuration;
5+
6+
public record BeanConfig(boolean eager, int creationTimeMs, int creationTotalTimeMs) {
7+
static final BeanConfig DEFAULT = new BeanConfig(false, -1, -1);
8+
9+
static BeanConfig fromAnnotationOrDefault(Bean annotation) {
10+
if (annotation == null) return DEFAULT;
11+
return new BeanConfig(annotation.eager(), annotation.creationTimeMs(), annotation.creationTotalTimeMs());
12+
}
13+
14+
static BeanConfig fromAnnotationOrDefault(Configuration annotation) {
15+
if (annotation == null) return DEFAULT;
16+
return new BeanConfig(annotation.eager(), annotation.creationTimeMs(), annotation.creationTotalTimeMs());
17+
}
18+
19+
BeanConfig withEager(boolean eager) {
20+
return new BeanConfig(eager, creationTimeMs, creationTotalTimeMs);
21+
}
22+
}

0 commit comments

Comments
 (0)