4
4
import dev .openfeature .contrib .providers .flagd .resolver .process .storage .connector .StreamPayloadType ;
5
5
import org .junit .jupiter .api .Test ;
6
6
7
+ import java .io .File ;
7
8
import java .io .IOException ;
9
+ import java .nio .file .Files ;
10
+ import java .nio .file .Path ;
11
+ import java .nio .file .StandardOpenOption ;
8
12
import java .time .Duration ;
9
13
import java .util .concurrent .BlockingQueue ;
10
14
11
15
import static dev .openfeature .contrib .providers .flagd .resolver .process .TestUtils .VALID_LONG ;
12
16
import static dev .openfeature .contrib .providers .flagd .resolver .process .TestUtils .getResourcePath ;
13
17
import static org .junit .jupiter .api .Assertions .assertEquals ;
14
18
import static org .junit .jupiter .api .Assertions .assertNotNull ;
15
- import static org .junit .jupiter .api .Assertions .assertThrows ;
16
19
import static org .junit .jupiter .api .Assertions .assertTimeoutPreemptively ;
17
20
18
21
class FileConnectorTest {
@@ -39,12 +42,65 @@ void readAndExposeFeatureFlagsFromSource() throws IOException {
39
42
}
40
43
41
44
@ Test
42
- void throwsErrorIfInvalidFile () {
45
+ void emitErrorStateForInvalidPath () throws IOException {
43
46
// given
44
47
final FileConnector connector = new FileConnector ("INVALID_PATH" );
45
48
49
+ // when
50
+ connector .init ();
51
+
52
+ // then
53
+ final BlockingQueue <StreamPayload > stream = connector .getStream ();
54
+
55
+ // Must emit an error within considerable time
56
+ final StreamPayload [] payload = new StreamPayload [1 ];
57
+ assertTimeoutPreemptively (Duration .ofMillis (200 ), () -> {
58
+ payload [0 ] = stream .take ();
59
+ });
60
+
61
+ assertNotNull (payload [0 ].getData ());
62
+ assertEquals (StreamPayloadType .ERROR , payload [0 ].getType ());
63
+ }
64
+
65
+ @ Test
66
+ void watchForFileUpdatesAndEmitThem () throws IOException {
67
+ final String initial = "{\" flags\" :{\" myBoolFlag\" :{\" state\" :\" ENABLED\" ,\" variants\" :{\" on\" :true," +
68
+ "\" off\" :false},\" defaultVariant\" :\" on\" }}}" ;
69
+
70
+ final String updatedFlags = "{\" flags\" :{\" myBoolFlag\" :{\" state\" :\" ENABLED\" ,\" variants\" :{\" on\" :true," +
71
+ "\" off\" :false},\" defaultVariant\" :\" off\" }}}" ;
72
+
73
+ // given
74
+ final File tmpFlagFile = File .createTempFile ("flagd" , "flags" );
75
+ final Path flagSourcePath = tmpFlagFile .toPath ();
76
+
77
+ Files .write (flagSourcePath , initial .getBytes (), StandardOpenOption .WRITE );
78
+ final FileConnector connector = new FileConnector (flagSourcePath .toString ());
79
+
80
+ // when
81
+ connector .init ();
82
+
46
83
// then
47
- assertThrows (IOException .class , connector ::init );
84
+ final BlockingQueue <StreamPayload > stream = connector .getStream ();
85
+
86
+ final StreamPayload [] payload = new StreamPayload [1 ];
87
+
88
+ // first validate the initial payload
89
+ assertTimeoutPreemptively (Duration .ofMillis (200 ), () -> {
90
+ payload [0 ] = stream .take ();
91
+ });
92
+
93
+ assertEquals (initial , payload [0 ].getData ());
94
+
95
+ // then update the flags
96
+ Files .write (flagSourcePath , updatedFlags .getBytes (), StandardOpenOption .WRITE );
97
+
98
+ // finally wait for updated payload
99
+ assertTimeoutPreemptively (Duration .ofSeconds (10 ), () -> {
100
+ payload [0 ] = stream .take ();
101
+ });
102
+
103
+ assertEquals (updatedFlags , payload [0 ].getData ());
48
104
}
49
105
50
106
}
0 commit comments