Skip to content

Commit

Permalink
Feat add gzip (#322)
Browse files Browse the repository at this point in the history
* format:format code

* feat:add gzip func

* fix:comment unuseful code

* fix: fix ret val and unit test assert

* fix:gzip unit modify

* fix:unit test param define modify

Co-authored-by: wangrui130 <wangrui130@tal.com>
Co-authored-by: 王瑞 <wangrui5@songguo7.com>
  • Loading branch information
3 people authored Oct 28, 2022
1 parent bb4c31e commit dd81e03
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 13 deletions.
48 changes: 48 additions & 0 deletions pkg/compressor/gzip_compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,51 @@
*/

package compressor

import (
"bytes"
"compress/gzip"
"io/ioutil"
)

type Gzip struct {
}

// Compress gzip compress
func (g *Gzip) Compress(b []byte) ([]byte, error) {
var buffer bytes.Buffer

gz := gzip.NewWriter(&buffer)

if _, err := gz.Write(b); err != nil {
return nil, err
}

if err := gz.Flush(); err != nil {
return nil, err
}

if err := gz.Close(); err != nil {
return nil, err
}

return buffer.Bytes(), nil
}

// Decompress gzip decompress
func (g *Gzip) Decompress(in []byte) ([]byte, error) {
reader, err := gzip.NewReader(bytes.NewReader(in))
if err != nil {
return nil, err
}

if err = reader.Close(); err != nil {
return nil, err
}

return ioutil.ReadAll(reader)
}

func (g *Gzip) GetCompressorType() CompressorType {
return CompressorGzip
}
40 changes: 40 additions & 0 deletions pkg/compressor/gzip_compress_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package compressor

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGzipCompress(t *testing.T) {
str := "test"

g := &Gzip{}

compressRes, err := g.Compress([]byte(str))
assert.NoError(t, err)
t.Logf("compress res: %v", string(compressRes))

decompressRes, err := g.Decompress(compressRes)
assert.NoError(t, err)
t.Logf("decompress res: %v", string(decompressRes))

assert.Equal(t, str, string(decompressRes))
}
7 changes: 0 additions & 7 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,3 @@
*/

package test

import (
"testing"
)

func Test1(t *testing.T) {
}
8 changes: 2 additions & 6 deletions test/rpc_remoting_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@

package test

import (
"testing"
)

func TestSendMsgWithResponse(test *testing.T) {
/*func TestSendMsgWithResponse(test *testing.T) {
//request := protocol.RegisterRMRequest{
// ResourceIds: "1111",
// AbstractIdentifyRequest: protocol.AbstractIdentifyRequest{
Expand All @@ -36,4 +32,4 @@ func TestSendMsgWithResponse(test *testing.T) {
//handler := GetGettyClientHandlerInstance()
//handler.sendMergedMessage(mergedMessage)
//time.Sleep(100000 * time.Second)
}
}*/

0 comments on commit dd81e03

Please sign in to comment.