TDD/ testing with streams in NodeJS
Made streamtest for that purpose. It not only make streams tests cleaner but also allows to test V1 and V2 streams https://www.npmjs.com/package/streamtest
Made streamtest for that purpose. It not only make streams tests cleaner but also allows to test V1 and V2 streams https://www.npmjs.com/package/streamtest
Very similar to @Eastsun’s, but a bit more intention revealing. Tested in Scala 2.8. scala> val l = List(1, 2, 3) l: List[Int] = List(1, 2, 3) scala> Stream.continually(l.toStream).flatten.take(10).toList res3: List[Int] = List(1, 2, 3, 1, 2, 3, 1, 2, 3, 1) Alternatively, with Scalaz: scala> import scalaz._ import scalaz._ scala> import Scalaz._ import Scalaz._ … Read more
In .NET 4.0 we finally got a Stream.CopyTo method! Yay!
Copy it into a new MemoryStream first. Then you can re-read the MemoryStream as many times as you like: Stream responseStream = CopyAndClose(resp.GetResponseStream()); // Do something with the stream responseStream.Position = 0; // Do something with the stream again private static Stream CopyAndClose(Stream inputStream) { const int readSize = 256; byte[] buffer = new byte[readSize]; … Read more
Something like this – put your output into a MemoryStream and then read that back in: public static string DataContractSerializeObject<T>(T objectToSerialize) { using(MemoryStream memStm = new MemoryStream()) { var serializer = new DataContractSerializer(typeof(T)); serializer.WriteObject(memStm, objectToSerialize); memStm.Seek(0, SeekOrigin.Begin); using(var streamReader = new StreamReader(memStm)) { string result = streamReader.ReadToEnd(); return result; } } }
When you return an IDisposable from a method, you are relegating the responsibility of disposing it to your caller. Thus, you need to declare your using block around the entire usage of the stream, which in your case presumably spans the UploadFile call. using (var s = GetFileStream()) UploadFile(s);
A write on a Socket can block too, especially if it is a TCP Socket. The OS will only buffer a certain amount of untransmitted (or transmitted but unacknowledged) data. If you write stuff faster than the remote app is able to read it, the socket will eventually back up and your write calls will … Read more
For many use cases where the best tool so far was Channel, Flow has become the new best tool. As a specific example, callbackFlow is now the best approach to receiving data from a 3rd-party API’s callback. This works especially well in a GUI setting. It couples the callback, a channel, and the associated receiving … Read more
Regular output (the actual result of running the program) should go on stdout, things like you mentioned (e.g. diagnostic, notice, warning, error) on stderr. If there is no “regular output”, I would say that it doesn’t really matter which one you choose. You could argue that the logging is the only output, so that should … Read more
They are two different APIs that allow you to access the same stream of data blocks. The ‘readable’ API was introduced in Node 0.10.0 as part of “Streams 2”, so if you search for that, it should help. The core of the issue is that the ‘readable’ interface allows for much simpler managing and buffering … Read more