Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ff0917f128156da9d101bf0c9f5ad9d87ae3642d (plain) (blame)
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
package org.eclipse.tm.internal.tcf.debug.tests;

import java.util.HashSet;
import java.util.Random;

import org.eclipse.tm.tcf.protocol.IChannel;
import org.eclipse.tm.tcf.protocol.IToken;
import org.eclipse.tm.tcf.services.IDiagnostics;
import org.eclipse.tm.tcf.services.IStreams;

class TestStreams implements ITCFTest, IStreams.StreamsListener {

    private final TCFTestSuite test_suite;
    private final IDiagnostics diag;
    private final IStreams streams;
    private final Random rnd = new Random();
    private final HashSet<String> stream_ids = new HashSet<String>();

    private String inp_id;
    private String out_id;

    private int test_count;

    TestStreams(TCFTestSuite test_suite, IChannel channel) {
        this.test_suite = test_suite;
        diag = channel.getRemoteService(IDiagnostics.class);
        streams = channel.getRemoteService(IStreams.class);
    }

    public void start() {
        if (diag == null ||streams == null) {
            test_suite.done(this, null);
        }
        else {
            subsrcibe();
        }
    }

    private void subsrcibe() {
        streams.subscribe(IDiagnostics.NAME, this, new IStreams.DoneSubscribe() {

            public void doneSubscribe(IToken token, Exception error) {
                if (error != null) {
                    exit(error);
                }
                else {
                    createStream();
                }
            }
        });
    }

    private void createStream() {
        diag.createTestStreams(1153, 947, new IDiagnostics.DoneCreateTestStreams() {

            public void doneCreateTestStreams(IToken token, Throwable error, String inp_id, String out_id) {
                if (error != null) {
                    exit(error);
                }
                else {
                    TestStreams.this.inp_id = inp_id;
                    TestStreams.this.out_id = out_id;
                    for (String id : stream_ids) {
                        if (id.equals(inp_id)) continue;
                        if (id.equals(out_id)) continue;
                        streams.disconnect(id, new IStreams.DoneDisconnect() {

                            public void doneDisconnect(IToken token, Exception error) {
                                if (error != null) {
                                    exit(error);
                                }
                            }
                        });
                    }
                    testReadWrite();
                }
            }
        });
    }

    private void testReadWrite() {
        final byte[] data_out = new byte[rnd.nextInt(10000) + 1000];
        new Random().nextBytes(data_out);
        final HashSet<IToken> cmds = new HashSet<IToken>();
        IStreams.DoneRead done_read = new IStreams.DoneRead() {

            private int offs = 0;
            private boolean eos;

            public void doneRead(IToken token, Exception error, int lost_size, byte[] data, boolean eos) {
                cmds.remove(token);
                if (error != null) {
                    if (!this.eos) {
                        exit(error);
                        return;
                    }
                }
                else if (lost_size != 0) {
                    exit(new Exception("Streams service: unexpected data loss"));
                    return;
                }
                else {
                    if (this.eos) {
                        if (!eos || data != null && data.length > 0) {
                            exit(new Exception("Streams service: unexpected successful read after EOS"));
                        }
                    }
                    else {
                        if (data != null) {
                            if (offs + data.length > data_out.length) {
                                exit(new Exception("Streams service: read returns more data then expected"));
                                return;
                            }
                            for (int n = 0; n < data.length; n++) {
                                if (data[n] != data_out[offs]) {
                                    exit(new Exception("Streams service: data error: " + data[n] + " != " + data_out[offs]));
                                    return;
                                }
                                offs++;
                            }
                        }
                        if (eos) {
                            if (offs != data_out.length) {
                                exit(new Exception("Streams service: unexpected EOS"));
                                return;
                            }
                            this.eos = true;
                        }
                        if (!this.eos && cmds.size() < 8) {
                            cmds.add(streams.read(out_id, 241, this));
                        }
                    }
                }
                if (cmds.isEmpty()) disposeStreams();
            }
        };
        cmds.add(streams.read(out_id, 223, done_read));
        cmds.add(streams.read(out_id, 227, done_read));
        cmds.add(streams.read(out_id, 229, done_read));
        cmds.add(streams.read(out_id, 233, done_read));

        IStreams.DoneWrite done_write = new IStreams.DoneWrite() {

            public void doneWrite(IToken token, Exception error) {
                if (error != null) exit(error);
            }
        };
        int offs = 0;
        while (offs < data_out.length) {
            int size = rnd.nextInt(400);
            if (size > data_out.length - offs) size = data_out.length - offs;
            streams.write(inp_id, data_out, offs, size, done_write);
            offs += size;
        }
        streams.eos(inp_id, new IStreams.DoneEOS() {

            public void doneEOS(IToken token, Exception error) {
                if (error != null) exit(error);
            }
        });
    }

    private void disposeStreams() {
        final HashSet<IToken> cmds = new HashSet<IToken>();
        IStreams.DoneDisconnect done_disconnect = new IStreams.DoneDisconnect() {

            public void doneDisconnect(IToken token, Exception error) {
                if (error != null) {
                    exit(error);
                }
                else {
                    cmds.remove(token);
                    if (cmds.isEmpty()) unsubscribe();
                }
            }
        };
        IDiagnostics.DoneDisposeTestStream done_dispose = new IDiagnostics.DoneDisposeTestStream() {

            public void doneDisposeTestStream(IToken token, Throwable error) {
                if (error != null) {
                    exit(error);
                }
                else {
                    cmds.remove(token);
                    if (cmds.isEmpty()) unsubscribe();
                }
            }
        };
        cmds.add(streams.disconnect(inp_id, done_disconnect));
        cmds.add(diag.disposeTestStream(inp_id, done_dispose));
        cmds.add(diag.disposeTestStream(out_id, done_dispose));
        cmds.add(streams.disconnect(out_id, done_disconnect));
    }

    private void unsubscribe() {
        streams.unsubscribe(IDiagnostics.NAME, this, new IStreams.DoneUnsubscribe() {

            public void doneUnsubscribe(IToken token, Exception error) {
                if (error != null || test_count >= 10) {
                    exit(error);
                }
                else {
                    test_count++;
                    stream_ids.clear();
                    inp_id = null;
                    out_id = null;
                    subsrcibe();
                }
            }
        });
    }

    private void exit(Throwable x) {
        if (!test_suite.isActive(this)) return;
        test_suite.done(this, x);
    }

    /************************** StreamsListener **************************/

    public void created(String stream_type, String stream_id, String context_id) {
        if (!IDiagnostics.NAME.equals(stream_type)) exit(new Exception("Invalid stream type in Streams.created event"));
        if (stream_ids.contains(stream_id)) exit(new Exception("Invalid stream ID in Streams.created event"));
        stream_ids.add(stream_id);
        if (inp_id != null) {
            if (inp_id.equals(stream_id)) exit(new Exception("Invalid stream ID in Streams.created event"));
            if (out_id.equals(stream_id)) exit(new Exception("Invalid stream ID in Streams.created event"));
            streams.disconnect(stream_id, new IStreams.DoneDisconnect() {

                public void doneDisconnect(IToken token, Exception error) {
                    if (error != null) {
                        exit(error);
                    }
                }
            });
        }
    }

    public void disposed(String stream_type, String stream_id) {
        if (!IDiagnostics.NAME.equals(stream_type)) exit(new Exception("Invalid stream type in Streams.disposed event"));
        if (!stream_ids.remove(stream_id)) exit(new Exception("Invalid stream ID in Streams.disposed event"));
    }
}

Back to the top