Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix compression and tests therefore #108

Merged
merged 7 commits into from
Jun 8, 2022
Merged

Conversation

tballard
Copy link
Contributor

@tballard tballard commented May 30, 2022

I changed the code to close some output streams so the server can actually get all the data. I added some tests using a local server so it could catch the compressed data, decompress it and send it back to be checked against the original.

Also the code was allowing the verb to be passed in as any case and then checking it by uppercasing and comparing whenever it was used. Except that somebody forgot to upper case it in one place which seemed like a bug. I changed the code to upper case it in one spot.

There were 4 small blocks of code that were trivially reduced to 1 and who could resist?

There were some existing compression tests that were failing once the change was made. The test was to make sure the response data had a specific value. One got the impression that someone just wrote the test after seeing the returned value, but who knows? I changed the tests to match the new returned values.

I uncommented some tests and they didn't blow up, so I left them. Maybe I went rogue ...

I don't think there is anything else of substance.

@tballard tballard closed this May 30, 2022
@tballard tballard reopened this May 30, 2022
@tballard tballard closed this May 30, 2022
@tballard tballard reopened this May 30, 2022
@@ -54,7 +54,7 @@ object Requester{
}
case class Requester(verb: String,
sess: BaseSession){

val CMD = verb.toUpperCase // allow submitting as lower case ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you adding CMD as a field here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"verb" was basically used as an enum, only without insisting it be case dependent. In general it was accessed using by comparing "POST" or something to verb.toUpperCase. One place in the code it was accessed without uppercasing, which I took to be a bug. Maybe I'm old-school, but I don't like code that does the same thing over and over even if the compiler will probably optimize it away, so I changed it to do the upper-casing once and compare to CMD. Maybe there is a style you prefer, but I would fix that bug in any case.

Copy link
Member

@lolgab lolgab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some first comments.

Comment on lines 224 to 220
for((k, v) <- blobHeaders) connection.setRequestProperty(k, v)

for((k, v) <- sess.headers) connection.setRequestProperty(k, v)

for((k, v) <- headers) connection.setRequestProperty(k, v)

for((k, v) <- compress.headers) connection.setRequestProperty(k, v)
Seq(blobHeaders, sess.headers, headers, compress.headers).
foreach(h => for((k, v) <- h) connection.setRequestProperty(k, v))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This creates an extra Seq that the old code was avoiding.
I would rollback it, also doesn't help fixing the bug you found.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, just a thought

Comment on lines 247 to 250
val bytes = new ByteArrayOutputStream()
withOs(compress.wrap(bytes)) { os => data.write(os) }
val byteArray = bytes.toByteArray
connection.setFixedLengthStreamingMode(byteArray.length)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting seems weird here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I see that, but suit yourself.

Comment on lines 361 to 369
/**
* Do something with an OutputStream and close it
* @param os OutputStream
* @param fn
*/
private def withOs[T](os: OutputStream)(fn: OutputStream => T) : Unit =
try fn(os) finally os.close()


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Scaladoc here doesn't add much value.
Also I suggest you to rename the method to usingOutputStream to be consistent with Scala 2.13's Using

Suggested change
/**
* Do something with an OutputStream and close it
* @param os OutputStream
* @param fn
*/
private def withOs[T](os: OutputStream)(fn: OutputStream => T) : Unit =
try fn(os) finally os.close()
private def usingOutputStream[T](os: OutputStream)(fn: OutputStream => T) : Unit =
try fn(os) finally os.close()

Comment on lines 33 to 34
test("send"){
requests.send("get")("https://httpbin.org/get?hello=world&foo=baz")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happened with formatting here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The formatting complaints are more obvious in the editor. Yeah, I can twiddle them.

Comment on lines 3 to 15
import java.net.InetSocketAddress

import com.sun.net.httpserver.HttpExchange
import com.sun.net.httpserver.HttpHandler
import com.sun.net.httpserver.HttpServer
import java.io._
import java.util.zip._
import scala.collection.mutable.StringBuilder
import utest._
import ujson._
import requests.Compress
import requests.Compress._
import scala.annotation.tailrec
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you organize these imports here?

// }
// }

//Tests fail with 'Request to https://httpbin.org/absolute-redirect/4 failed with status code 404'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you fix those tests? If so you should remove this comment, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd buy that

Comment on lines 196 to 204
"https://httpbin.org/post",
compress = requests.Compress.None,
data = new RequestBlob.ByteSourceRequestBlob("Hello World")
)
assert(res1.text.contains(""""Hello World""""))

val res2 = requests.post(
"https://httpbin.org/post",
compress = requests.Compress.Gzip,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting is weird here

Comment on lines 298 to 305
/**
* Compress with each compression mode and call server. Server expands
* and passes it back so we can compare
*/
test("compressionData") {
import Compress._
val str = "I am deflater mouse"
Seq(None, Gzip, Deflate).foreach(c =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting is weird here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you could be slightly more specific on what is weird where you don't like the formatting. One of then looks a little over-indented, but I wouldn't know what to change on the others. I've never done this before, and in my imagination you were going to merge in the parts you found agreeable and hit format;-)

Comment on lines 5 to 7
import com.sun.net.httpserver.HttpExchange
import com.sun.net.httpserver.HttpHandler
import com.sun.net.httpserver.HttpServer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work on Java 11+ ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I tested for all versions. I can try again ...

@lolgab
Copy link
Member

lolgab commented Jun 8, 2022

Hi @tballard,
I did some changes myself, are you fine enabling "Allow changes from maintainers"?
In that case I could push some commits to your branch.
You could also approve and merge (with Rebase Strategy) this PR tballard#1

@lolgab lolgab self-requested a review June 8, 2022 16:48
Copy link
Member

@lolgab lolgab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!
Sorry for the long turnaround!
Good job on the fix and on setting up a server for testing!

@lolgab lolgab merged commit 7e18f64 into com-lihaoyi:master Jun 8, 2022
@lihaoyi lihaoyi mentioned this pull request Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants