| 82 | | |
|---|
| 83 | | |
|---|
| 84 | | @with_emptyfile() |
|---|
| 85 | | @with_chunkfile_ofzeros(32 * 3 + 8, 32) |
|---|
| 86 | | def test_encode_filetosocket__decode_socket_tofile(**kw): |
|---|
| 87 | | |
|---|
| 88 | | CL, CHKSZ, PENCHKSZ = 32 * 3 + 8, 32, 8 |
|---|
| 89 | | srcfile = kw.pop('chunkfile') |
|---|
| 90 | | dstfile = kw.pop('emptyfile') |
|---|
| 91 | | clientsock, serversock = socketpair() |
|---|
| 92 | | clientsock.setblocking(0) |
|---|
| 93 | | serversock.setblocking(0) |
|---|
| 94 | | chunksz = CHKSZ |
|---|
| 95 | | server_recv_buffer_size = len(hex(chunksz)[2:]) + chunksz + 2 * len('\r\n') |
|---|
| 96 | | encoder = encode_file_tosocket(srcfile, clientsock, chunksz) |
|---|
| 97 | | decoder = decode_socket_tofile(serversock, dstfile, server_recv_buffer_size) |
|---|
| 98 | | nsent = 0 |
|---|
| 99 | | for i in range(0, 5): |
|---|
| 100 | | eval = encoder.next() |
|---|
| 101 | | while eval is clientsock: |
|---|
| 102 | | print 'client blocked ...' |
|---|
| 103 | | eval = encoder.next() |
|---|
| 104 | | nsent += 1 |
|---|
| 105 | | print 'sent %d' % nsent |
|---|
| 106 | | dval = decoder.next() |
|---|
| 107 | | if dval is serversock: |
|---|
| 108 | | print 'server blocked ...' |
|---|
| 109 | | continue |
|---|
| 110 | | |
|---|
| 111 | | if nsent < 4: |
|---|
| 112 | | assert dval == CHKSZ, '%s != %s' % (dval, CHKSZ) |
|---|
| 113 | | elif nsent == 4: |
|---|
| 114 | | assert dval == PENCHKSZ, '%s != %s' % (dval, PENCHKSZ) |
|---|
| 115 | | elif nsent == 5: |
|---|
| 116 | | assert dval == 0, '"%s" != %s' % (dval, 0) |
|---|
| 117 | | else: |
|---|
| 118 | | # we expect the last recv to have no remainder |
|---|
| 119 | | assert not dval, '%s != ""' % str(dval) |
|---|
| 120 | | final = decoder.next() |
|---|
| 121 | | assert final == "" or final == buffer(""), ( |
|---|
| 122 | | 'expected the empty string as the final yield, got "%s"' |
|---|
| 123 | | ) % str(final) |
|---|
| 124 | | dstfile.flush() |
|---|
| 125 | | assert dstfile.tell() == CL, '%s != %d' % (str(dstfile.tell()), CL) |
|---|
| 126 | | try: |
|---|
| 127 | | print decoder.next() |
|---|
| 128 | | assert False, 'decoder not exhausted but file is fully recvd' |
|---|
| 129 | | except StopIteration, e: |
|---|
| 130 | | pass |
|---|