1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.eviware.soapui.impl.wsdl.monitor;
18
19 /***
20 * class to simulate slow connections by slowing down the system
21 */
22 public class SlowLinkSimulator
23 {
24
25 /***
26 * Field delayBytes
27 */
28 private int delayBytes;
29
30 /***
31 * Field delayTime
32 */
33 private int delayTime;
34
35 /***
36 * Field currentBytes
37 */
38 private int currentBytes;
39
40 /***
41 * Field totalBytes
42 */
43 private int totalBytes;
44
45 /***
46 * construct
47 *
48 * @param delayBytes
49 * bytes per delay; set to 0 for no delay
50 * @param delayTime
51 * delay time per delay in milliseconds
52 */
53 public SlowLinkSimulator( int delayBytes, int delayTime )
54 {
55 this.delayBytes = delayBytes;
56 this.delayTime = delayTime;
57 }
58
59 /***
60 * construct by copying delay bytes and time, but not current count of bytes
61 *
62 * @param that
63 * source of data
64 */
65 public SlowLinkSimulator( SlowLinkSimulator that )
66 {
67 this.delayBytes = that.delayBytes;
68 this.delayTime = that.delayTime;
69 }
70
71 /***
72 * how many bytes have gone past?
73 *
74 * @return integer
75 */
76 public int getTotalBytes()
77 {
78 return totalBytes;
79 }
80
81 /***
82 * log #of bytes pumped. Will pause when necessary. This method is not
83 * synchronized
84 *
85 * @param bytes
86 */
87 public void pump( int bytes )
88 {
89 totalBytes += bytes;
90 if( delayBytes == 0 )
91 {
92
93
94 return;
95 }
96 currentBytes += bytes;
97 if( currentBytes > delayBytes )
98 {
99
100
101 int delaysize = currentBytes / delayBytes;
102 long delay = delaysize * ( long ) delayTime;
103
104
105 currentBytes = currentBytes % delayBytes;
106
107
108 try
109 {
110 Thread.sleep( delay );
111 }
112 catch( InterruptedException e )
113 {
114
115 }
116 }
117 }
118
119 /***
120 * get the current byte count
121 *
122 * @return integer
123 */
124 public int getCurrentBytes()
125 {
126 return currentBytes;
127 }
128
129 /***
130 * set the current byte count
131 *
132 * @param currentBytes
133 */
134 public void setCurrentBytes( int currentBytes )
135 {
136 this.currentBytes = currentBytes;
137 }
138 }