text:
package region

import (
	"bytes"
	"context"
	"encoding/binary"
	"encoding/json"
	"errors"
	"fmt"
	"math"
	"net"
	"reflect"
	"strconv"
	"strings"
	"sync"
	"sync/atomic"
	"testing"
	"time"

	"github.com/golang/mock/gomock"
	"github.com/stretchr/testify/assert"
	"github.com/tsuna/gohbase/hrpc"
	"github.com/tsuna/gohbase/pb"
	"github.com/tsuna/gohbase/test"
	"github.com/tsuna/gohbase/test/mock"
	"google.golang.org/protobuf/encoding/protowire"
	"google.golang.org/protobuf/proto"
)

func TestErrors(t *testing.T) {
	ue := ServerError{fmt.Errorf("oops")}
	if ue.Error() != "region.ServerError: oops" {
		t.Errorf("Wrong error message. Got %q, wanted %q", ue, "region.ServerError: oops")
	}
}

func TestWrite(t *testing.T) {
	ctrl := test.NewController(t)
	defer ctrl.Finish()
	mockConn := mock.NewMockConn(ctrl)
	c := &client{
		conn: mockConn,
	}

	// check if Write returns an error
	expectErr := errors.New("nope")
	mockConn.EXPECT().Write(gomock.Any()).Return(0, expectErr).Times(1)
	err := c.write([]byte("lol"))
	if err != expectErr {
		t.Errorf("expected %v, got %v", expectErr, err)
	}

	// check if it actually writes the right data
	expected := []byte("lol")
	mockConn.EXPECT().Write(gomock.Any()).Return(3, nil).Times(1).Do(func(buf []byte) {
		if !bytes.Equal(expected, buf) {
			t.Errorf("expected %v, got %v", expected, buf)
		}
	})
	err = c.write(expected)
	if err != nil {
		t.Errorf("Was expecting error, but got one: %#v", err)
	}
}

func TestSendHello(t *testing.T) {
	ctrl := test.NewController(t)
	defer ctrl.Finish()
	mockConn := mock.NewMockConn(ctrl)
	c := &client{
		conn:          mockConn,
		effectiveUser: "root",
		ctype:         RegionClient,
	}

	// check if it's sending the right "hello" for RegionClient
	mockConn.EXPECT().Write(gomock.Any()).Return(78, nil).Times(1).Do(func(buf []byte) {
		expected := []byte("HBas\x00P\x00\x00\x00D\n\x06\n\x04root\x12\rClientService\x1a+" +
			"org.apache.hadoop.hbase.codec.KeyValueCodec")
		if !bytes.Equal(expected, buf) {
			t.Errorf("expected %v, got %v", expected, buf)
		}
	})
	err := c.sendHello()
	if err != nil {
		t.Errorf("Wasn't expecting error, but got one: %#v", err)
	}

	// check if it sends the right "hello" for MasterClient
	c.ctype = MasterClient
	mockConn.EXPECT().Write(gomock.Any()).Return(78, nil).Times(1).Do(func(buf []byte) {
		expected := []byte("HBas\x00P\x00\x00\x00D\n\x06\n\x04root\x12\rMasterService\x1a+" +
			"org.apache.hadoop.hbase.codec.KeyValueCodec")
		if !bytes.Equal(expected, buf) {
			t.Errorf("expected %v, got %v", expected, buf)
		}
	})
	err = c.sendHello()
	if err != nil {
		t.Errorf("Was expecting error, but got one: %#v", err)
	}

	c.compressor = &compressor{Codec: mockCodec{}}
	// check if compressor is getting sent
	c.ctype = RegionClient
	mockConn.EXPECT().Write(gomock.Any()).Return(78, nil).Times(1).Do(func(buf []byte) {
		expected := []byte("HBas\x00P\x00\x00\x00J\n\x06\n\x04root\x12\rClientService\x1a+" +
			"org.apache.hadoop.hbase.codec.KeyValueCodec\"\x04mock")
		if !bytes.Equal(expected, buf) {
			t.Errorf("expected %q, got %q", expected, buf)
		}
	})
	err = c.sendHello()
	if err != nil {
		t.Errorf("Was expecting error, but got one: %#v", err)
	}
}

func TestFail(t *testing.T) {
	ctrl := test.NewController(t)
	defer ctrl.Finish()
	mockConn := mock.NewMockConn(ctrl)
	c := &client{
		conn: mockConn,
		done: make(chan struct{}),
		rpcs: make(chan hrpc.Call),
		sent: make(map[uint32]hrpc.Call),
	}
	expectedErr := errors.New("oooups")

	// check that connection Close is called only once
	mockConn.EXPECT().Close().Times(1)

	// run multiple in parallel to make sure everything is called only once
	var wg sync.WaitGroup
	for i := 0; i < 100; i++ {
		wg.Add(1)
		go func() {
			c.fail(expectedErr)
			wg.Done()
		}()
	}
	wg.Wait()

	// check if done channel is closed to notify goroutines to stop
	// if close(c.done) is called more than once, it would panic
	select {
	case <-time.After(2 * time.Second):
		t.Errorf("done hasn't been closed")
	case _, more := <-c.done:
		if more {
			t.Error("expected done to be closed")
		}
	}

	// check that failing undialed client doesn't panic
	c = &client{
		done: make(chan struct{}),
		rpcs: make(chan hrpc.Call),
		sent: make(map[uint32]hrpc.Call),
	}
	c.fail(expectedErr)
}

type mc struct {
	*mock.MockConn

	closed uint32
}

func (c *mc) Write(b []byte) (n int, err error) {
	if v := atomic.LoadUint32(&c.closed); v != 0 {
		return 0, errors.New("closed connection")
	}
	return 0, nil
}

func (c *mc) Close() error {
	atomic.AddUint32(&c.closed, 1)
	return nil
}

func TestQueueRPCMultiWithClose(t *testing.T) {
	ctrl := test.NewController(t)
	defer ctrl.Finish()

	mockConn := mock.NewMockConn(ctrl)
	mockConn.EXPECT().SetReadDeadline(gomock.Any()).AnyTimes()

	ncalls := 1000

	c := &client{
		conn:          &mc{MockConn: mockConn},
		rpcs:          make(chan hrpc.Call),
		done:          make(chan struct{}),
		sent:          make(map[uint32]hrpc.Call),
		rpcQueueSize:  10,
		flushInterval: 30 * time.Millisecond,
	}

	var wgProcessRPCs sync.WaitGroup
	wgProcessRPCs.Add(1)
	go func() {
		c.processRPCs()
		wgProcessRPCs.Done()
	}()

	calls := make([]hrpc.Call, ncalls)
	for i := range calls {
		call, err := hrpc.NewGet(context.Background(), []byte("yolo"), []byte("swag"))
		if err != nil {
			t.Fatal(err)
		}
		call.SetRegion(reg0)
		calls[i] = call
	}

	for _, call := range calls {
		go c.QueueRPC(call)
	}

	c.Close()

	// check that all calls are not stuck and get an error
	for _, call := range calls {
		r := <-call.ResultChan()
		if r.Error == nil {
			t.Error("got no error")
		}
	}

	wgProcessRPCs.Wait()
}

type rpcMatcher struct {
	payload []byte
}

func (m rpcMatcher) Matches(x interface{}) bool {
	data, ok := x.([]byte)
	if !ok {
		return false
	}
	return bytes.HasSuffix(data, m.payload)
}

func (m rpcMatcher) String() string {
	return fmt.Sprintf("RPC payload is equal to %q", m.payload)
}

func newRPCMatcher(payload []byte) gomock.Matcher {
	return rpcMatcher{payload: payload}
}

func TestProcessRPCsWithFail(t *testing.T) {
	ctrl := test.NewController(t)
	defer ctrl.Finish()

	mockConn := mock.NewMockConn(ctrl)
	mockConn.EXPECT().Close()

	ncalls := 100

	c := &client{
		conn: mockConn,
		rpcs: make(chan hrpc.Call),
		done: make(chan struct{}),
		sent: make(map[uint32]hrpc.Call),
		// never send anything
		rpcQueueSize:  ncalls + 1,
		flushInterval: 1000 * time.Hour,
	}

	var wgProcessRPCs sync.WaitGroup
	wgProcessRPCs.Add(1)
	go func() {
		c.processRPCs()
		wgProcessRPCs.Done()
	}()

	calls := make([]hrpc.Call, ncalls)
	for i := range calls {
		call, err := hrpc.NewGet(context.Background(), []byte("yolo"), []byte("swag"))
		if err != nil {
			t.Fatal(err)
		}
		call.SetRegion(reg0)
		calls[i] = call
	}

	for _, call := range calls {
		c.QueueRPC(call)
	}

	c.fail(errors.New("OOOPS"))

	// check that all calls are not stuck and get an error
	for _, call := range calls {
		r := <-call.ResultChan()
		if r.Error != ErrClientClosed {
			t.Errorf("got unexpected error %v, expected %v", r.Error, ErrClientClosed)
		}
	}

	wgProcessRPCs.Wait()
}

func mockRPCProto(row string) (proto.Message, []byte) {
	regionType := pb.RegionSpecifier_REGION_NAME
	r := &pb.RegionSpecifier{
		Type:  &regionType,
		Value: []byte("yolo"),
	}
	get := &pb.GetRequest{Region: r, Get: &pb.Get{Row: []byte(row)}}

	var b []byte
	var err error
	b = protowire.AppendVarint(b, uint64(proto.Size(get)))
	b, err = proto.MarshalOptions{}.MarshalAppend(b, get)
	if err != nil {
		panic(err)
	}
	return get, b
}

func TestQueueRPC(t *testing.T) {
	ctrl := test.NewController(t)
	defer ctrl.Finish()

	queueSize := 30
	flushInterval := 20 * time.Millisecond
	mockConn := mock.NewMockConn(ctrl)
	mockConn.EXPECT().SetReadDeadline(gomock.Any()).AnyTimes()
	c := &client{
		conn:          mockConn,
		rpcs:          make(chan hrpc.Call),
		done:          make(chan struct{}),
		sent:          make(map[uint32]hrpc.Call),
		rpcQueueSize:  queueSize,
		flushInterval: flushInterval,
	}
	var wgProcessRPCs sync.WaitGroup
	wgProcessRPCs.Add(1)
	go func() {
		c.processRPCs() // Writer goroutine
		wgProcessRPCs.Done()
	}()

	// define rpcs behaviour
	var wgWrites sync.WaitGroup
	calls := make([]hrpc.Call, 100)
	ctx := context.Background()
	for i := range calls {
		wgWrites.Add(1)
		mockCall := mock.NewMockCall(ctrl)
		mockCall.EXPECT().Name().Return("Get").Times(1)
		p, payload := mockRPCProto(fmt.Sprintf("rpc_%d", i))
		mockCall.EXPECT().ToProto().Return(p).Times(1)
		mockCall.EXPECT().Context().Return(ctx).AnyTimes()
		mockCall.EXPECT().Description().AnyTimes()
		mockCall.EXPECT().ResultChan().Return(make(chan hrpc.RPCResult, 1)).Times(1)
		calls[i] = mockCall

		// we expect that it eventually writes to connection
		mockConn.EXPECT().Write(newRPCMatcher(payload)).Times(1).Return(14+len(payload), nil).Do(
			func(buf []byte) { wgWrites.Done() })
	}

	// queue calls in parallel
	for _, call := range calls {
		go func(call hrpc.Call) {
			c.QueueRPC(call)
		}(call)
	}

	// wait till all writes complete
	wgWrites.Wait()

	// check sent map
	if len(c.sent) != len(calls) {
		t.Errorf("expected len(c.sent) be %d, got %d", len(calls), len(c.sent))
	}

	var wg sync.WaitGroup
	// now we fail the regionserver, and try to queue stuff
	mockConn.EXPECT().Close().Times(1)
	c.fail(errors.New("ooups"))
	for i := 0; i < 100; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			result := make(chan hrpc.RPCResult, 1)
			mockCall := mock.NewMockCall(ctrl)
			mockCall.EXPECT().Context().Return(ctx).AnyTimes()
			mockCall.EXPECT().Description().AnyTimes()
			mockCall.EXPECT().ResultChan().Return(result).Times(1)
			c.QueueRPC(mockCall)
			r := <-result
			err, ok := r.Error.(ServerError)
			if !ok {
				t.Errorf("Expected ServerError error")
				return
			}
			if ErrClientClosed != err {
				t.Errorf("expected %v, got %v", ErrClientClosed, err)
			}
		}()
	}
	wg.Wait()
	wgProcessRPCs.Wait()
}

func TestServerErrorWrite(t *testing.T) {
	ctrl := test.NewController(t)
	defer ctrl.Finish()

	queueSize := 1
	flushInterval := 10 * time.Millisecond
	mockConn := mock.NewMockConn(ctrl)
	c := &client{
		conn:          mockConn,
		rpcs:          make(chan hrpc.Call),
		done:          make(chan struct{}),
		sent:          make(map[uint32]hrpc.Call),
		rpcQueueSize:  queueSize,
		flushInterval: flushInterval,
	}
	// define rpcs behaviour
	mockCall := mock.NewMockCall(ctrl)
	p, payload := mockRPCProto("rpc")
	mockCall.EXPECT().ToProto().Return(p).Times(1)
	mockCall.EXPECT().Name().Return("Get").Times(1)
	mockCall.EXPECT().Context().Return(context.Background()).AnyTimes()
	mockCall.EXPECT().Description().AnyTimes()
	result := make(chan hrpc.RPCResult, 1)
	mockCall.EXPECT().ResultChan().Return(result).Times(1)
	// we expect that it eventually writes to connection
	expErr := errors.New("Write failure")
	mockConn.EXPECT().Write(newRPCMatcher(payload)).Times(1).Return(0, expErr)
	mockConn.EXPECT().Close()

	c.QueueRPC(mockCall)
	// check that processRPCs exists
	c.processRPCs()
	r := <-result
	if ErrClientClosed != r.Error {
		t.Errorf("expected %v, got %v", ErrClientClosed, r.Error)
	}
	if len(c.sent) != 0 {
		t.Errorf("Expected all awaiting rpcs to be processed, %d left", len(c.sent))
	}
}

func TestServerErrorRead(t *testing.T) {
	ctrl := test.NewController(t)
	defer ctrl.Finish()

	queueSize := 1
	flushInterval := 10 * time.Millisecond
	mockConn := mock.NewMockConn(ctrl)
	c := &client{
		conn:          mockConn,
		rpcs:          make(chan hrpc.Call),
		done:          make(chan struct{}),
		sent:          make(map[uint32]hrpc.Call),
		rpcQueueSize:  queueSize,
		flushInterval: flushInterval,
	}
	// define rpcs behavior
	mockCall := mock.NewMockCall(ctrl)
	result := make(chan hrpc.RPCResult, 1)
	mockCall.EXPECT().ResultChan().Return(result).Times(1)
	mockConn.EXPECT().Read([]byte{0, 0, 0, 0}).Return(0, errors.New("read failure"))
	mockConn.EXPECT().Close()

	// pretend we already unqueued and sent the rpc
	c.sent[1] = mockCall
	// now try receiving result, should call fail
	c.receiveRPCs()
	_, more := <-c.done
	if more {
		t.Error("expected done to be closed")
	}
	// finish reading from c.rpc to clean up the c.sent map
	c.processRPCs()
	if len(c.sent) != 0 {
		t.Errorf("Expected all awaiting rpcs to be processed, %d left", len(c.sent))
	}
	r := <-result
	if ErrClientClosed != r.Error {
		t.Errorf("expected %v, got %v", ErrClientClosed, r.Error)
	}
}

func TestServerErrorExceptionResponse(t *testing.T) {
	ctrl := test.NewController(t)
	defer ctrl.Finish()
	mockConn := mock.NewMockConn(ctrl)
	mockConn.EXPECT().SetReadDeadline(gomock.Any()).Times(2)
	c := &client{
		conn:          mockConn,
		rpcs:          make(chan hrpc.Call),
		done:          make(chan struct{}),
		sent:          make(map[uint32]hrpc.Call),
		rpcQueueSize:  1,
		flushInterval: 1000 * time.Second,
	}

	rpc, err := hrpc.NewGetStr(context.Background(), "test1", "yolo")
	if err != nil {
		t.Fatalf("Failed to create Get request: %s", err)
	}

	c.registerRPC(rpc)
	if err := c.inFlightUp(); err != nil {
		t.Fatal(err)
	}

	var response []byte
	header := &pb.ResponseHeader{
		CallId: proto.Uint32(1),
		Exception: &pb.ExceptionResponse{
			ExceptionClassName: proto.String(
				"org.apache.hadoop.hbase.regionserver.RegionServerAbortedException"),
			StackTrace: proto.String("ooops"),
		},
	}

	response = protowire.AppendVarint(response, uint64(proto.Size(header)))
	response, err = proto.MarshalOptions{}.MarshalAppend(response, header)
	if err != nil {
		t.Fatal(err)
	}

	mockConn.EXPECT().Read(readBufSizeMatcher{l: 4}).Times(1).Return(4, nil).
		Do(func(buf []byte) { binary.BigEndian.PutUint32(buf, uint32(len(response))) })

	mockConn.EXPECT().Read(readBufSizeMatcher{l: len(response)}).Times(1).
		Return(len(response), nil).Do(func(buf []byte) { copy(buf, response) })

	expErr := exceptionToError(
		"org.apache.hadoop.hbase.regionserver.RegionServerAbortedException", "ooops")

	err = c.receive()
	if _, ok := err.(ServerError); !ok {
		if err.Error() != expErr.Error() {
			t.Fatalf("expected ServerError with message %q, got %T: %v", expErr, err, err)
		}
	}

	re := <-rpc.ResultChan()
	if re.Error != err {
		t.Errorf("expected error %v, got %v", err, re.Error)
	}
}

func TestExceptionToError(t *testing.T) {
	tcases := []struct {
		class string
		stack string
		out   error
	}{
		{
			class: "java.io.IOException",
			stack: "ooops",
			out:   errors.New("HBase Java exception java.io.IOException:\nooops"),
		},
		{
			class: "java.io.IOException",
			stack: "Cannot append; log is closed\nblahblah",
			out: NotServingRegionError{errors.New("HBase Java exception java.io.IOException:\n" +
				"Cannot append; log is closed\nblahblah")},
		},
		{
			class: "org.apache.hadoop.hbase.CallQueueTooBigException",
			stack: "blahblah",
			out: RetryableError{errors.New(
				"HBase Java exception org.apache.hadoop.hbase.CallQueueTooBigException:\n" +
					"blahblah")},
		},
	}
	for _, tcase := range tcases {
		t.Run(tcase.class, func(t *testing.T) {
			err := exceptionToError(tcase.class, tcase.stack)
			if !reflect.DeepEqual(err, tcase.out) {
				t.Fatalf("expected error %q, got error %q", tcase.out, err)
			}
		})
	}
}

func TestReceiveDecodeProtobufError(t *testing.T) {
	ctrl := test.NewController(t)
	defer ctrl.Finish()

	mockConn := mock.NewMockConn(ctrl)
	c := &client{
		conn: mockConn,
		done: make(chan struct{}),
		sent: make(map[uint32]hrpc.Call),
	}

	mockCall := mock.NewMockCall(ctrl)
	result := make(chan hrpc.RPCResult, 1)
	mockCall.EXPECT().ResultChan().Return(result).Times(1)
	mockCall.EXPECT().NewResponse().Return(&pb.MutateResponse{}).Times(1)
	mockCall.EXPECT().Context().Return(context.Background()).Times(1)

	c.sent[1] = mockCall
	c.inFlight = 1

	// Append mutate response with a chunk in the middle missing
	response := []byte{6, 8, 1, 26, 2, 8, 38, 34, 0, 0, 0, 22,
		0, 0, 0, 4, 0, 4, 121, 111, 108, 111, 2, 99, 102, 115, 119, 97, 103, 0, 0, 0, 0, 0, 0,
		0, 0, 4, 109, 101, 111, 119}
	mockConn.EXPECT().Read(readBufSizeMatcher{l: 4}).Times(1).Return(4, nil).
		Do(func(buf []byte) { binary.BigEndian.PutUint32(buf, uint32(len(response))) })
	mockConn.EXPECT().Read(readBufSizeMatcher{l: len(response)}).Times(1).
		Return(len(response), nil).Do(func(buf []byte) { copy(buf, response) })
	mockConn.EXPECT().SetReadDeadline(time.Time{}).Times(1)
	expErrorPefix := "region.RetryableError: failed to decode the response: proto:"

	err := c.receive()
	if err == nil || strings.HasPrefix(expErrorPefix, err.Error()) {
		t.Errorf("Expected error prefix %v, got %v", expErrorPefix, err)
	}

	res := <-result
	if err != res.Error {
		t.Errorf("Expected error %v, got %v", err, res.Error)
	}
}

type callWithCellBlocksError struct{ hrpc.Call }

func (cwcbe callWithCellBlocksError) DeserializeCellBlocks(proto.Message, []byte) (uint32, error) {
	return 0, errors.New("OOPS")
}

func TestReceiveDeserializeCellblocksError(t *testing.T) {
	ctrl := test.NewController(t)
	defer ctrl.Finish()

	mockConn := mock.NewMockConn(ctrl)
	c := &client{
		conn: mockConn,
		done: make(chan struct{}),
		sent: make(map[uint32]hrpc.Call),
	}

	mockCall := mock.NewMockCall(ctrl)
	result := make(chan hrpc.RPCResult, 1)
	mockCall.EXPECT().ResultChan().Return(result).Times(1)
	mockCall.EXPECT().NewResponse().Return(&pb.MutateResponse{}).Times(1)
	mockCall.EXPECT().Context().Return(context.Background()).Times(1)

	c.sent[1] = callWithCellBlocksError{mockCall}
	c.inFlight = 1

	// Append mutate response
	response := []byte{6, 8, 1, 26, 2, 8, 38, 6, 10, 4, 16, 1, 32, 0, 0, 0, 0, 34, 0, 0, 0, 22,
		0, 0, 0, 4, 0, 4, 121, 111, 108, 111, 2, 99, 102, 115, 119, 97, 103, 0, 0, 0, 0, 0, 0,
		0, 0, 4, 109, 101, 111, 119}
	mockConn.EXPECT().Read(readBufSizeMatcher{l: 4}).Times(1).Return(4, nil).
		Do(func(buf []byte) { binary.BigEndian.PutUint32(buf, uint32(len(response))) })
	mockConn.EXPECT().Read(readBufSizeMatcher{l: len(response)}).Times(1).
		Return(len(response), nil).Do(func(buf []byte) { copy(buf, response) })
	mockConn.EXPECT().SetReadDeadline(time.Time{}).Times(1)
	expError := errors.New("region.RetryableError: failed to decode the response: OOPS")

	err := c.receive()
	if err == nil || err.Error() != expError.Error() {
		t.Errorf("Expected error %v, got %v", expError, err)
	}

	res := <-result
	if err != res.Error {
		t.Errorf("Expected error %v, got %v", err, res.Error)
	}
}

func TestUnexpectedSendError(t *testing.T) {
	ctrl := test.NewController(t)
	defer ctrl.Finish()

	queueSize := 1
	flushInterval := 10 * time.Millisecond
	mockConn := mock.NewMockConn(ctrl)
	c := &client{
		conn:          mockConn,
		rpcs:          make(chan hrpc.Call),
		done:          make(chan struct{}),
		sent:          make(map[uint32]hrpc.Call),
		rpcQueueSize:  queueSize,
		flushInterval: flushInterval,
	}
	go c.processRPCs()
	// define rpcs behaviour
	mockCall := mock.NewMockCall(ctrl)
	mockCall.EXPECT().ToProto().Return(nil).Times(1)
	mockCall.EXPECT().Context().Return(context.Background()).AnyTimes()
	mockCall.EXPECT().Description().AnyTimes()
	result := make(chan hrpc.RPCResult, 1)
	mockCall.EXPECT().ResultChan().Return(result).Times(1)
	mockCall.EXPECT().Name().Return("Whatever").Times(1)

	c.QueueRPC(mockCall)
	r := <-result
	expectedErr := "failed to marshal request: proto: Marshal called with nil"
	if err := r.Error; err == nil || err.Error() != expectedErr {
		t.Errorf("expected %q, got %v", expectedErr, err)
	}
	if len(c.sent) != 0 {
		t.Errorf("Expected all awaiting rpcs to be processed, %d left", len(c.sent))
	}
	// stop the go routine
	mockConn.EXPECT().Close()
	c.Close()
}

func TestProcessRPCs(t *testing.T) {
	ctrl := test.NewController(t)
	defer ctrl.Finish()

	tests := []struct {
		qsize      int
		interval   time.Duration
		ncalls     int
		minsent    int
		maxsent    int
		concurrent bool
	}{
		{qsize: 100000, interval: 30 * time.Millisecond, ncalls: 100,
			minsent: 1, maxsent: 1},
		{qsize: 2, interval: 1000 * time.Hour, ncalls: 100, minsent: 50, maxsent: 50},
		{qsize: 100, interval: 0 * time.Millisecond, ncalls: 1000,
			minsent: 1, maxsent: 1000, concurrent: true},
	}

	for i, tcase := range tests {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			mockConn := mock.NewMockConn(ctrl)
			mockConn.EXPECT().Close()

			c := &client{
				conn:          mockConn,
				rpcs:          make(chan hrpc.Call),
				done:          make(chan struct{}),
				sent:          make(map[uint32]hrpc.Call),
				rpcQueueSize:  tcase.qsize,
				flushInterval: tcase.interval,
			}

			var wgProcessRPCs sync.WaitGroup
			wgProcessRPCs.Add(1)
			go func() {
				c.processRPCs()
				wgProcessRPCs.Done()
			}()

			var sent int32
			var wgWrite sync.WaitGroup
			wgWrite.Add(tcase.minsent)
			mockConn.EXPECT().Write(gomock.Any()).MinTimes(tcase.minsent).
				MaxTimes(tcase.maxsent).Return(42, nil).Do(func(buf []byte) {
				if atomic.AddInt32(&sent, 1) <= int32(tcase.minsent) {
					wgWrite.Done() // test will timeout if didn't get at least minsent
				}
			})
			mockConn.EXPECT().SetReadDeadline(gomock.Any()).
				MinTimes(tcase.minsent).MaxTimes(tcase.maxsent)

			calls := make([]hrpc.Call, tcase.ncalls)
			for i := range calls {
				call, err := hrpc.NewGet(context.Background(), []byte("yolo"), []byte("swag"))
				if err != nil {
					t.Fatal(err)
				}
				call.SetRegion(reg0)
				calls[i] = call
			}

			if tcase.concurrent {
				var wg sync.WaitGroup
				for _, call := range calls {
					wg.Add(1)
					go func(call hrpc.Call) {
						c.QueueRPC(call)
						wg.Done()
					}(call)
				}
				wg.Wait()
			} else {
				for _, call := range calls {
					c.QueueRPC(call)
				}
			}

			wgWrite.Wait()

			c.Close()
			wgProcessRPCs.Wait()
			t.Log("num batches sent", sent)

			if f := int(c.inFlight); f < tcase.minsent || f > tcase.maxsent {
				t.Errorf("expected [%d:%d] in-flight rpcs, got %d",
					tcase.minsent, tcase.maxsent, c.inFlight)
			}
		})
	}
}

func TestRPCContext(t *testing.T) {
	ctrl := test.NewController(t)
	defer ctrl.Finish()
	queueSize := 10
	flushInterval := 1000 * time.Second
	mockConn := mock.NewMockConn(ctrl)
	mockConn.EXPECT().Close()
	c := &client{
		conn:          mockConn,
		rpcs:          make(chan hrpc.Call),
		done:          make(chan struct{}),
		sent:          make(map[uint32]hrpc.Call),
		rpcQueueSize:  queueSize,
		flushInterval: flushInterval,
	}

	mockConn.EXPECT().SetReadDeadline(gomock.Any()).Times(1)

	// queue rpc with background context
	mockCall := mock.NewMockCall(ctrl)
	mockCall.EXPECT().Name().Return("Get").Times(1)
	p, payload := mockRPCProto("yolo")
	mockCall.EXPECT().ToProto().Return(p).Times(1)
	mockCall.EXPECT().Context().Return(context.Background()).AnyTimes()
	mockCall.EXPECT().Description().AnyTimes()
	mockCall.EXPECT().ResultChan().Return(make(chan hrpc.RPCResult, 1)).Times(1)
	mockConn.EXPECT().Write(newRPCMatcher(payload)).Times(1).Return(14+len(payload), nil)
	c.QueueRPC(mockCall)

	ctxCancel, cancel := context.WithCancel(context.Background())
	cancel()
	callWithCancel := mock.NewMockCall(ctrl)
	callWithCancel.EXPECT().Context().Return(ctxCancel).AnyTimes()
	mockCall.EXPECT().Description().AnyTimes()
	// this shouldn't block
	c.QueueRPC(callWithCancel)

	if int(c.inFlight) != 1 {
		t.Errorf("expected %d in-flight rpcs, got %d", 1, c.inFlight)
	}
	// clean up
	c.Close()
}

type readBufSizeMatcher struct {
	l int
}

func (r readBufSizeMatcher) Matches(x interface{}) bool {
	data, ok := x.([]byte)
	if !ok {
		return false
	}
	return len(data) == r.l
}

func (r readBufSizeMatcher) String() string {
	return fmt.Sprintf("buf size is equal to %q", r.l)
}

func TestSanity(t *testing.T) {
	ctrl := test.NewController(t)
	defer ctrl.Finish()
	mockConn := mock.NewMockConn(ctrl)
	c := &client{
		conn:          mockConn,
		rpcs:          make(chan hrpc.Call),
		done:          make(chan struct{}),
		sent:          make(map[uint32]hrpc.Call),
		rpcQueueSize:  1, // size one to skip sendBatch
		flushInterval: 1000 * time.Second,
	}

	var wg sync.WaitGroup

	wg.Add(1)
	go func() {
		c.processRPCs()
		wg.Done()
	}()

	// using Append as it returns cellblocks
	app, err := hrpc.NewAppStr(context.Background(), "test1", "yolo",
		map[string]map[string][]byte{"cf": map[string][]byte{"swag": []byte("meow")}})
	if err != nil {
		t.Fatalf("Failed to create Get request: %s", err)
	}
	app.SetRegion(
		NewInfo(0, nil, []byte("test1"), []byte("test1,,lololololololololololo"), nil, nil))

	mockConn.EXPECT().Write(gomock.Any()).Times(2).Return(0, nil)
	mockConn.EXPECT().SetReadDeadline(gomock.Any()).Times(1)

	c.QueueRPC(app)

	response := []byte{6, 8, 1, 26, 2, 8, 38, 6, 10, 4, 16, 1, 32, 0, 0, 0, 0, 34, 0, 0, 0, 22,
		0, 0, 0, 4, 0, 4, 121, 111, 108, 111, 2, 99, 102, 115, 119, 97, 103, 0, 0, 0, 0, 0, 0,
		0, 0, 4, 109, 101, 111, 119}
	mockConn.EXPECT().Read(readBufSizeMatcher{l: 4}).Times(1).Return(4, nil).
		Do(func(buf []byte) {
			binary.BigEndian.PutUint32(buf, uint32(len(response)))
		})
	mockConn.EXPECT().Read(readBufSizeMatcher{l: len(response)}).Times(1).
		Return(len(response), nil).Do(func(buf []byte) {
		copy(buf, response)

		// stall the next read
		mockConn.EXPECT().Read(readBufSizeMatcher{l: 4}).MaxTimes(1).
			Return(0, errors.New("closed")).Do(func(buf []byte) { <-c.done })
	})
	mockConn.EXPECT().SetReadDeadline(time.Time{}).Times(1)
	wg.Add(1)
	go func() {
		c.receiveRPCs()
		wg.Done()
	}()

	re := <-app.ResultChan()
	if re.Error != nil {
		t.Error(re.Error)
	}
	r, ok := re.Msg.(*pb.MutateResponse)
	if !ok {
		t.Fatalf("got unexpected type %T for response", r)
	}
	expResult := &pb.Result{
		AssociatedCellCount: proto.Int32(1),
		Stale:               proto.Bool(false),
		Cell: []*pb.Cell{
			&pb.Cell{
				Row:       []byte("yolo"),
				Family:    []byte("cf"),
				Qualifier: []byte("swag"),
				Value:     []byte("meow"),
				CellType:  pb.CellType_PUT.Enum(),
				Timestamp: proto.Uint64(0),
			},
		},
	}
	if !proto.Equal(expResult, r.Result) {
		t.Errorf("expected %v, got %v", expResult, r.Result)
	}
	if int(c.inFlight) != 0 {
		t.Errorf("expected %d in-flight rpcs, got %d", 0, c.inFlight)
	}

	mockConn.EXPECT().Close().Times(1)
	c.Close()
	wg.Wait()
}

func TestSanityCompressor(t *testing.T) {
	ctrl := test.NewController(t)
	defer ctrl.Finish()
	mockConn := mock.NewMockConn(ctrl)
	c := &client{
		conn:          mockConn,
		rpcs:          make(chan hrpc.Call),
		done:          make(chan struct{}),
		sent:          make(map[uint32]hrpc.Call),
		rpcQueueSize:  1, // size one to skip sendBatch
		flushInterval: 1000 * time.Second,
		compressor:    &compressor{Codec: mockCodec{}},
	}

	var wg sync.WaitGroup

	wg.Add(1)
	go func() {
		c.processRPCs()
		wg.Done()
	}()

	// using Append as it returns cellblocks
	app, err := hrpc.NewAppStr(context.Background(), "test1", "yolo",
		map[string]map[string][]byte{"cf": map[string][]byte{"swag": []byte("meow")}})
	if err != nil {
		t.Fatalf("Failed to create Get request: %s", err)
	}
	app.SetRegion(
		NewInfo(0, nil, []byte("test1"), []byte("test1,,lololololololololololo"), nil, nil))

	compressedCellblocks := "\x00\x00\x00&" +
		"\x00\x00\x00\n\x00\x00\x00\"\x00\x00\x00\x16\x00\x00" +
		"\x00\x00\x00\n\x00\x04\x00\x04yolo\x02c" +
		"\x00\x00\x00\nfswag\u007f\xff\xff\xff\xff" +
		"\x00\x00\x00\b\xff\xff\xff\x04meow"

	mockConn.EXPECT().Write([]byte("\x00\x00\x00}\x10\b\x01\x1a\x06Mutate \x01*\x02\b:1\n!"+
		"\b\x01\x12\x1dtest1,,lololololololololololo\x12\f\n\x04yolo\x10\x000\x00@\x01")).Return(
		71, nil)
	mockConn.EXPECT().Write([]byte(compressedCellblocks)).Return(58, nil)
	mockConn.EXPECT().SetReadDeadline(gomock.Any())

	c.QueueRPC(app)

	header := &pb.ResponseHeader{
		CallId: proto.Uint32(1),
		CellBlockMeta: &pb.CellBlockMeta{
			Length: proto.Uint32(58),
		},
	}

	mutate := &pb.MutateResponse{
		Result: &pb.Result{
			AssociatedCellCount: proto.Int32(1),
			Stale:               proto.Bool(false),
		},
	}

	var response []byte
	response = protowire.AppendVarint(response, uint64(proto.Size(header)))
	response, err = proto.MarshalOptions{}.MarshalAppend(response, header)
	if err != nil {
		t.Fatal(err)
	}
	response = protowire.AppendVarint(response, uint64(proto.Size(mutate)))
	response, err = proto.MarshalOptions{}.MarshalAppend(response, mutate)
	if err != nil {
		t.Fatal(err)
	}

	response = append(response, []byte(compressedCellblocks)...)
	mockConn.EXPECT().Read(readBufSizeMatcher{l: 4}).Times(1).Return(4, nil).
		Do(func(buf []byte) {
			binary.BigEndian.PutUint32(buf, uint32(len(response)))
		})
	mockConn.EXPECT().Read(readBufSizeMatcher{l: len(response)}).Times(1).
		Return(len(response), nil).Do(func(buf []byte) {
		copy(buf, response)

		// stall the next read
		mockConn.EXPECT().Read(readBufSizeMatcher{l: 4}).MaxTimes(1).
			Return(0, errors.New("closed")).Do(func(buf []byte) { <-c.done })
	})
	mockConn.EXPECT().SetReadDeadline(time.Time{}).Times(1)
	wg.Add(1)
	go func() {
		c.receiveRPCs()
		wg.Done()
	}()

	re := <-app.ResultChan()
	if re.Error != nil {
		t.Error(re.Error)
	}
	r, ok := re.Msg.(*pb.MutateResponse)
	if !ok {
		t.Fatalf("got unexpected type %T for response", r)
	}
	expResult := &pb.Result{
		AssociatedCellCount: proto.Int32(1),
		Stale:               proto.Bool(false),
		Cell: []*pb.Cell{
			&pb.Cell{
				Row:       []byte("yolo"),
				Family:    []byte("cf"),
				Qualifier: []byte("swag"),
				Value:     []byte("meow"),
				CellType:  pb.CellType_PUT.Enum(),
				Timestamp: proto.Uint64(math.MaxInt64),
			},
		},
	}
	if !proto.Equal(expResult, r.Result) {
		t.Errorf("expected %v, got %v", expResult, r.Result)
	}
	if int(c.inFlight) != 0 {
		t.Errorf("expected %d in-flight rpcs, got %d", 0, c.inFlight)
	}

	mockConn.EXPECT().Close().Times(1)
	c.Close()
	wg.Wait()
}

func BenchmarkSendBatchMemory(b *testing.B) {
	ctrl := test.NewController(b)
	defer ctrl.Finish()
	mockConn := mock.NewMockConn(ctrl)
	c := &client{
		conn: mockConn,
		rpcs: make(chan hrpc.Call),
		done: make(chan struct{}),
		sent: make(map[uint32]hrpc.Call),
		// queue size is 1 so that all QueueRPC calls trigger sendBatch,
		// and buffer slice reset
		rpcQueueSize:  1,
		flushInterval: 1000 * time.Second,
	}

	var wgWrites sync.WaitGroup
	ctx := context.Background()
	mockCall := mock.NewMockCall(ctrl)
	mockCall.EXPECT().Name().Return("Get").AnyTimes()
	p, _ := mockRPCProto("rpc")
	mockCall.EXPECT().ToProto().Return(p).AnyTimes()
	mockCall.EXPECT().Context().Return(ctx).AnyTimes()
	mockConn.EXPECT().Write(gomock.Any()).AnyTimes().Return(0, nil).Do(func(buf []byte) {
		wgWrites.Done()
	})
	mockConn.EXPECT().SetReadDeadline(gomock.Any()).AnyTimes()

	go c.processRPCs()
	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		for i := 0; i < 100; i++ {
			wgWrites.Add(1)
			c.QueueRPC(mockCall)
		}
		wgWrites.Wait()
	}
	// we don't care about cleaning up
}

func BenchmarkSetReadDeadline(b *testing.B) {
	l, err := net.Listen("tcp", "localhost:0")
	if err != nil {
		b.Fatal(err)
	}
	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		conn, err := l.Accept()
		if err != nil {
			b.Error(err)
		}
		wg.Done()
		conn.Close()
	}()

	conn, err := net.Dial("tcp", l.Addr().String())
	if err != nil {
		b.Fatal(err)
	}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		if err := conn.SetReadDeadline(time.Now().Add(DefaultReadTimeout)); err != nil {
			b.Fatal(err)
		}
	}
	b.StopTimer()

	conn.Close()
	l.Close()
	wg.Wait()
}

func TestBuffer(t *testing.T) {
	size := 42
	b := newBuffer(size)
	if cap(b) < size {
		t.Fatalf("Excpected cap >= %d, got %d", size, cap(b))
	}
	if len(b) != size {
		t.Fatalf("Excpected len %d, got %d", size, len(b))
	}
	freeBuffer(b)

	size = 40
	b = newBuffer(size)
	if cap(b) < size {
		t.Fatalf("Excpected cap >= %d, got %d", size, cap(b))
	}
	if len(b) != size {
		t.Fatalf("Excpected len %d, got %d", size, len(b))
	}
	freeBuffer(b)

	size = 45
	b = newBuffer(size)
	if cap(b) < size {
		t.Fatalf("Excpected cap >= %d, got %d", size, cap(b))
	}
	if len(b) != size {
		t.Fatalf("Excpected len %d, got %d", size, len(b))
	}
	freeBuffer(b)
}

func TestMarshalJSON(t *testing.T) {
	ctrl := test.NewController(t)
	defer ctrl.Finish()

	var localAddr net.Addr
	var remoteAddr net.Addr
	var id uint32 = 111
	tcp := "tcp"
	localIp := []byte("172.16.254.1")
	remoteIp := []byte("10.16.254.1")
	localAddr = &net.TCPAddr{IP: localIp, Port: 0, Zone: "testZone"}
	remoteAddr = &net.TCPAddr{IP: remoteIp, Port: 0, Zone: "testZone"}

	mockConn := mock.NewMockConn(ctrl)
	mockConn.EXPECT().LocalAddr().Return(localAddr)
	mockConn.EXPECT().RemoteAddr().Return(remoteAddr)
	c := &client{
		conn:          mockConn,
		addr:          "testRegionServerAddress",
		ctype:         RegionClient,
		rpcs:          make(chan hrpc.Call),
		done:          make(chan struct{}),
		sent:          make(map[uint32]hrpc.Call),
		rpcQueueSize:  1, // size one to skip sendBatch
		flushInterval: 1000 * time.Second,
		compressor:    &compressor{Codec: mockCodec{}},
		id:            id,
	}

	jsonVal, err := c.MarshalJSON()

	if err != nil {
		t.Fatalf("Did not expect Error to be thrown: %v", err)
	}

	var jsonUnMarshal map[string]interface{}
	err = json.Unmarshal(jsonVal, &jsonUnMarshal)

	if err != nil {
		t.Fatalf("Error while unmarshalling JSON, %v", err)
	}

	actualLocalAddr := jsonUnMarshal["ConnectionLocalAddress"].(map[string]interface{})
	actualRemoteAddr := jsonUnMarshal["ConnectionRemoteAddress"].(map[string]interface{})

	assert.Equal(t, tcp, actualLocalAddr["Network"])
	assert.Equal(t, tcp, actualRemoteAddr["Network"])
	assert.Equal(t, string(RegionClient), jsonUnMarshal["ClientType"])
	assert.Equal(t, float64(0), jsonUnMarshal["InFlight"])
	assert.Equal(t, float64(id), jsonUnMarshal["Id"])

}

func TestMarshalJSONNilValues(t *testing.T) {
	ctrl := test.NewController(t)
	defer ctrl.Finish()

	c := &client{
		conn: nil,
		rpcs: nil,
		done: nil,
		sent: nil,
	}

	_, err := c.MarshalJSON()
	if err != nil {
		t.Fatalf("Did not expect Error to be thrown: %v", err)
	}
}


text:
package com.coolweather.android.util;

import android.text.TextUtils;

import com.coolweather.android.db.City;
import com.coolweather.android.db.County;
import com.coolweather.android.db.Province;
import com.coolweather.android.gson.Weather;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by shengli.zhang on 2017/3/13.
 */

public class Utility {

    /**
     * 解析和处理服务器返回的省级数据
     *
     * @param response 返回的数据
     * @return
     */
    public static boolean handleProvinceResponse(String response) {
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allProvince = new JSONArray(response);
                for (int i = 0; i < allProvince.length(); i++) {
                    Province province = new Province();
                    JSONObject obj = allProvince.getJSONObject(i);
                    province.setProvinceName(obj.getString("name"));
                    province.setProvinceCode(obj.getInt("id"));
                    province.save();
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 解析和处理服务器返回的市级数据
     *
     * @param response
     * @param provinceId
     * @return
     */
    public static boolean handleCityResponse(String response, int provinceId) {
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allCity = new JSONArray(response);
                for (int i = 0; i < allCity.length(); i++) {
                    JSONObject obj = allCity.getJSONObject(i);
                    City city = new City();
                    city.setCityName(obj.getString("name"));
                    city.setCityCode(obj.getInt("id"));
                    city.setProvinceId(provinceId);
                    city.save();
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 解析和处理服务器返回的县级数据
     *
     * @param response
     * @param cityId
     * @return
     */
    public static boolean handleCountyResponse(String response, int cityId) {
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allCounty = new JSONArray(response);
                for (int i = 0; i < allCounty.length(); i++) {
                    JSONObject obj = allCounty.getJSONObject(i);
                    County county = new County();
                    county.setCountyName(obj.getString("name"));
                    county.setWeatherId(obj.getString("weather_id"));
                    county.setCityId(cityId);
                    county.save();
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 将返回的JSON数据解析成Weather实体类
     *
     * @param response
     * @return
     */
    public static Weather handleWeatherResponse(String response) {
        try {
            JSONObject obj = new JSONObject(response);
            JSONArray jsonArray = obj.getJSONArray("HeWeather");
            String weatherContent = jsonArray.getJSONObject(0).toString();
            return new Gson().fromJson(weatherContent, Weather.class);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}

redpajama_set_name: RedPajamaGithub


text:
from pyspark.statcounter import StatCounter
from pyspark.testing.utils import ReusedPySparkTestCase

class StatCounterTests(ReusedPySparkTestCase):
    def test_base(self):
        stats = self.sc.parallelize([1.0, 2.0, 3.0, 4.0]).stats()
        self.assertEqual(stats.count(), 4)
        self.assertEqual(stats.max(), 4.0)
        self.assertEqual(stats.mean(), 2.5)
        self.assertEqual(stats.min(), 1.0)
        self.assertAlmostEqual(stats.stdev(), 1.118033988749895)
        self.assertAlmostEqual(stats.sampleStdev(), 1.2909944487358056)
        self.assertEqual(stats.sum(), 10.0)
        self.assertAlmostEqual(stats.variance(), 1.25)
        self.assertAlmostEqual(stats.sampleVariance(), 1.6666666666666667)

    def test_as_dict(self):
        stats = self.sc.parallelize([1.0, 2.0, 3.0, 4.0]).stats().asDict()
        self.assertEqual(stats["count"], 4)
        self.assertEqual(stats["max"], 4.0)
        self.assertEqual(stats["mean"], 2.5)
        self.assertEqual(stats["min"], 1.0)
        self.assertAlmostEqual(stats["stdev"], 1.2909944487358056)
        self.assertEqual(stats["sum"], 10.0)
        self.assertAlmostEqual(stats["variance"], 1.6666666666666667)

        stats = self.sc.parallelize([1.0, 2.0, 3.0, 4.0]).stats().asDict(sample=True)
        self.assertEqual(stats["count"], 4)
        self.assertEqual(stats["max"], 4.0)
        self.assertEqual(stats["mean"], 2.5)
        self.assertEqual(stats["min"], 1.0)
        self.assertAlmostEqual(stats["stdev"], 1.118033988749895)
        self.assertEqual(stats["sum"], 10.0)
        self.assertAlmostEqual(stats["variance"], 1.25)

    def test_merge(self):
        stats = StatCounter([1.0, 2.0, 3.0, 4.0])
        stats.merge(5.0)
        self.assertEqual(stats.count(), 5)
        self.assertEqual(stats.max(), 5.0)
        self.assertEqual(stats.mean(), 3.0)
        self.assertEqual(stats.min(), 1.0)
        self.assertAlmostEqual(stats.stdev(), 1.414213562373095)
        self.assertAlmostEqual(stats.sampleStdev(), 1.5811388300841898)
        self.assertEqual(stats.sum(), 15.0)
        self.assertAlmostEqual(stats.variance(), 2.0)
        self.assertAlmostEqual(stats.sampleVariance(), 2.5)

    def test_merge_stats(self):
        stats1 = StatCounter([1.0, 2.0, 3.0, 4.0])
        stats2 = StatCounter([1.0, 2.0, 3.0, 4.0])
        stats = stats1.mergeStats(stats2)
        self.assertEqual(stats.count(), 8)
        self.assertEqual(stats.max(), 4.0)
        self.assertEqual(stats.mean(), 2.5)
        self.assertEqual(stats.min(), 1.0)
        self.assertAlmostEqual(stats.stdev(), 1.118033988749895)
        self.assertAlmostEqual(stats.sampleStdev(), 1.1952286093343936)
        self.assertEqual(stats.sum(), 20.0)
        self.assertAlmostEqual(stats.variance(), 1.25)
        self.assertAlmostEqual(stats.sampleVariance(), 1.4285714285714286)

    def test_merge_stats_with_self(self):
        stats = StatCounter([1.0, 2.0, 3.0, 4.0])
        stats.mergeStats(stats)
        self.assertEqual(stats.count(), 8)
        self.assertEqual(stats.max(), 4.0)
        self.assertEqual(stats.mean(), 2.5)
        self.assertEqual(stats.min(), 1.0)
        self.assertAlmostEqual(stats.stdev(), 1.118033988749895)
        self.assertAlmostEqual(stats.sampleStdev(), 1.1952286093343936)
        self.assertEqual(stats.sum(), 20.0)
        self.assertAlmostEqual(stats.variance(), 1.25)
        self.assertAlmostEqual(stats.sampleVariance(), 1.4285714285714286)

if __name__ == "__main__":
    import unittest
    from pyspark.tests.test_statcounter import *  # noqa: F401

    try:
        import xmlrunner  # type: ignore[import]

        testRunner = xmlrunner.XMLTestRunner(output="target/test-reports", verbosity=2)
    except ImportError:
        testRunner = None
    unittest.main(testRunner=testRunner, verbosity=2)


text:
import { expect } from 'chai';
import sinon from 'sinon';

import getInjectableClassNameToggler from './get-injectable-class-name-toggler';
import { WebviewHandler } from './webview-handler';
import prefillUsername from './webview-handler-prefill-username';
import * as webviewOnReady from './webview-on-ready';

describe('lib/WebviewHandler', () => {
  const sandbox = sinon.createSandbox();

  beforeEach(() => {
    window.requestIdleCallback = window.requestIdleCallback || (() => {});
  });

  afterEach(() => {
    sandbox.restore();
  });

  describe('create', () => {
    it('should return an appropriate WebviewHandler instance', () => {
      const webviewHandler = WebviewHandler.create();

      expect(webviewHandler.container).to.equal(null);
      expect(webviewHandler.addedWebviews).to.eql([]);
      expect(webviewHandler.queuedCommands).to.eql([]);
    });
  });

  describe('attachTo', () => {
    let webviewHandler;
    let mockElem;

    beforeEach(() => {
      webviewHandler = WebviewHandler.create();
      mockElem = { setAttribute: sinon.spy() };
    });

    it('should set container property', () => {
      webviewHandler.attachTo(mockElem);

      expect(webviewHandler.container).to.equal(mockElem);
    });

    it('should hide the webviewHandler', () => {
      sinon.spy(webviewHandler, 'hide');
      webviewHandler.attachTo(mockElem);

      expect(webviewHandler.hide).to.have.been.calledWith();
    });

    it('should run queued commands', () => {
      const queuedCommands = [
        sinon.spy(),
        sinon.spy(),
      ];
      webviewHandler.queuedCommands = queuedCommands;
      webviewHandler.attachTo(mockElem);

      queuedCommands
        .forEach(cmd => expect(cmd).to.have.been.calledWith());
    });
  });

  describe('show', () => {
    let webviewHandler;

    beforeEach(() => {
      webviewHandler = WebviewHandler.create();
    });

    it('should queue command and return if container doesnt exist', () => {
      sinon.spy(webviewHandler, '_queueCommand');
      webviewHandler.show();

      expect(webviewHandler._queueCommand).to.have.been.calledOnce;
    });

    it('should set container style', () => {
      const mockContainer = { setAttribute: sinon.spy() };
      webviewHandler.container = mockContainer;

      webviewHandler.show();

      expect(mockContainer.setAttribute).to.have.been.calledWith('style', sinon.match(/relative/g));
    });
  });

  describe('hide', () => {
    let webviewHandler;

    beforeEach(() => {
      webviewHandler = WebviewHandler.create();
    });

    it('should set container style to hide it', () => {
      const mockContainer = { setAttribute: sinon.spy() };
      webviewHandler.container = mockContainer;

      webviewHandler.hide();

      expect(mockContainer.setAttribute).to.have.been.calledWith('style', sinon.match(/absolute/));
    });
  });

  describe('reload', () => {
    let webviewHandler;

    beforeEach(() => {
      webviewHandler = WebviewHandler.create();
    });

    it('should call reload upon the webview', () => {
      const mockWebview = { reload: sinon.spy() };
      const name = 'albert';
      sinon.stub(webviewHandler, '_getWebview').returns(mockWebview);

      webviewHandler.reload(name);
      expect(webviewHandler._getWebview).to.have.been.calledWith(name);
      expect(mockWebview.reload).to.have.been.calledWith();
    });
  });

  describe('displayView', () => {
    let webviewHandler;

    beforeEach(() => {
      webviewHandler = WebviewHandler.create();
      webviewHandler.container = document.createElement('div');
    });

    it('should queue command and return if container doesnt exist', () => {
      sinon.spy(webviewHandler, '_queueCommand');
      webviewHandler.container = null;

      webviewHandler.displayView('name');

      expect(webviewHandler._queueCommand).to.have.been.called;
    });

    it('should set accurate visibility value to webviews that sould be hidden', () => {
      const name = 'sigrid';
      const webviewsToBeHidden = [document.createElement('webview'), document.createElement('webview')];
      const webviews = [document.createElement('webview'), ...webviewsToBeHidden];
      sinon.stub(webviewHandler, '_getWebview').returns(webviews[0]);
      sinon.stub(webviewHandler.container, 'querySelectorAll').returns(webviews);
      webviewHandler.addedWebviews = [name];

      webviewHandler.displayView(name);

      webviewsToBeHidden
        .forEach(webview => {
          expect(webview.style.visibility).to.equal('hidden');
          expect(webview.getAttribute('data-active')).to.equal(null);
        });
    });

    it('should set accurate visibility value webview that should be displayed', () => {
      const name = 'sigrid';
      const webviewToBeDisplayed = document.createElement('webview');
      const webviews = [document.createElement('webview')].concat(webviewToBeDisplayed);
      sinon.stub(webviewHandler, '_getWebview').returns(webviewToBeDisplayed);
      sinon.stub(webviewHandler.container, 'querySelectorAll').returns(webviews);
      sinon.spy(webviewToBeDisplayed, 'focus');
      webviewHandler.addedWebviews = [name];

      webviewHandler.displayView(name);

      expect(webviewToBeDisplayed.style.visibility).to.equal('visible');
      expect(webviewToBeDisplayed.getAttribute('data-active')).to.equal('true');
    });

    it('should inject JS to toggle classList upon dom-ready', () => {
      sandbox.stub(webviewOnReady, 'default');
      const webview = Object.assign(document.createElement('webview'), {
        executeJavaScript: () => null,
      });

      sinon.stub(webviewHandler, '_getWebview').returns(webview);
      sinon.stub(webview, 'addEventListener');
      sinon.stub(webview, 'executeJavaScript');
      const classNames = {
        lisa: false,
        mats: true,
      };

      webviewHandler.displayView('random', { classNames });

      expect(webviewOnReady.default).to.have.been.calledWith(webview);
      expect(webview.executeJavaScript).to.not.have.been.called;

      webviewOnReady.default.getCalls()
        .forEach(call => call.args[1]());
      expect(webview.executeJavaScript).to.have.been.calledWith(
        getInjectableClassNameToggler(classNames)
      );
    });

    it('should focus webview upon dom-ready', () => {
      sandbox.stub(webviewOnReady, 'default');
      const mockWebview = document.createElement('webview');
      sinon.spy(mockWebview, 'focus');
      sinon.stub(webviewHandler, '_getWebview').returns(mockWebview);

      webviewHandler.displayView('alfred');

      webviewOnReady.default.getCalls()
        .forEach(call => call.args[1]());

      expect(mockWebview.focus).to.have.been.calledWith();
    });
  });

  describe('_getWebview', () => {
    let webviewHandler;

    beforeEach(() => {
      webviewHandler = WebviewHandler.create();
      webviewHandler.container = document.createElement('div');
    });

    it('should return correct element', () => {
      const name = 'astrid';
      sinon.spy(webviewHandler.container, 'querySelector');

      webviewHandler._getWebview(name);

      expect(webviewHandler.container.querySelector).to.have.been.calledWith(`[data-name='${name}']`);
    });
  });

  describe('addWebview', () => {
    const sandbox = sinon.createSandbox();
    let webviewHandler;

    beforeEach(() => {
      webviewHandler = WebviewHandler.create();
      webviewHandler.container = { appendChild: sinon.spy() };
    });

    afterEach(() => {
      sandbox.restore();
    });

    it('should queue command and return if container doesnt exist', () => {
      sandbox.spy(webviewHandler, '_queueCommand');
      webviewHandler.container = null;

      webviewHandler.addWebview('name');

      expect(webviewHandler._queueCommand).to.have.been.called;
    });

    it('should create webview element and append to container', () => {
      const mockElem = {
        setAttribute: sinon.spy(),
        addEventListener: sinon.spy(),
      };
      const name = 'beatrice';
      sandbox.stub(document, 'createElement').returns(mockElem);
      const url =  'https://something.protonmail.com/';

      const webview = webviewHandler.addWebview(name, url);

      expect(webview).to.equal(mockElem);

      expect(document.createElement).to.have.been.calledWith('webview');
      expect(mockElem.setAttribute).to.have.been.calledWith('src', url);
      expect(mockElem.setAttribute).to.have.been.calledWith('style', sinon.match(/absolute/));
      expect(mockElem.setAttribute).to.have.been.calledWith('data-name', name);
      expect(mockElem.setAttribute).to.have.been.calledWith('preload', sinon.match(/^file:\/\//));

      expect(webviewHandler.container.appendChild).to.have.been.calledWith(mockElem);
      expect(webviewHandler.addedWebviews).to.eql([name]);
    });

    it('should inject custom CSS when the DOM is ready', () => {
      const mockElem = {
        addEventListener: sinon.spy(),
        executeJavaScript: sinon.spy(),
        insertCSS: sinon.spy(),
        removeEventListener: sinon.spy(),
        setAttribute: sinon.spy(),
      };
      sandbox.stub(document, 'createElement').returns(mockElem);

      const webview = webviewHandler.addWebview('lars');

      expect(webview.addEventListener).to.have.been.calledWith('dom-ready', sinon.match.func);

      const insertCSSFn = webview.addEventListener.getCalls()
        .filter(call => call.args[0] === 'dom-ready')
        .map(call => call.args[1])
        .pop();
      insertCSSFn();

      expect(webview.insertCSS).to.have.been.calledWith(sinon.match.any, 'utf8');
      expect(webview.removeEventListener).to.have.been.calledWith('dom-ready', insertCSSFn);
    });

    it('should prefill the username when the DOM is ready', () => {
      const mockElem = {
        addEventListener: sinon.spy(),
        executeJavaScript: sinon.spy(),
        insertCSS: sinon.spy(),
        removeEventListener: sinon.spy(),
        setAttribute: sinon.spy(),
      };
      const username = 'anders';
      sandbox.stub(document, 'createElement').returns(mockElem);

      const webview = webviewHandler.addWebview(username);

      expect(webview.addEventListener).to.have.been.calledWith('dom-ready', sinon.match.func);

      const domReadyFn = webview.addEventListener.getCalls()
        .filter(call => call.args[0]=== 'dom-ready')
        .map(call => call.args[1])
        .pop();
      domReadyFn();

      expect(webview.executeJavaScript).to.have.been.calledWith('('.concat(prefillUsername, `('${username}')`, ')'));
      expect(webview.removeEventListener).to.have.been.calledWith('dom-ready', domReadyFn);
    });
  });

  describe('focusActive', () => {
    const sandbox = sinon.createSandbox();
    let webviewHandler;

    beforeEach(() => {
      webviewHandler = WebviewHandler.create();
    });

    afterEach(() => {
      sandbox.restore();
    });

    it('should call focus on accurate element', () => {
      const mockElem = { focus: sinon.spy() };
      sandbox.stub(document, 'querySelector').returns(mockElem);
      webviewHandler.focusActive();

      expect(mockElem.focus).to.have.been.calledOnce;
    });
  });
});


text:
from __future__ import division

from math import log, ceil, floor
import os
import re
from subprocess import Popen, PIPE
import sys
from tempfile import TemporaryFile
from warnings import warn

try:
    import audioop
except ImportError:
    import pyaudioop as audioop

if sys.version_info >= (3, 0):
    basestring = str

FRAME_WIDTHS = {
    8: 1,
    16: 2,
    32: 4,
}
ARRAY_TYPES = {
    8:  "b",
    16: "h",
    32: "i",
}
ARRAY_RANGES = {
    8: (-0x80, 0x7f),
    16: (-0x8000, 0x7fff),
    32: (-0x80000000, 0x7fffffff),
}

def get_frame_width(bit_depth):
    return FRAME_WIDTHS[bit_depth]

def get_array_type(bit_depth, signed=True):
    t = ARRAY_TYPES[bit_depth]
    if not signed:
        t = t.upper()
    return t

def get_min_max_value(bit_depth):
    return ARRAY_RANGES[bit_depth]

def _fd_or_path_or_tempfile(fd, mode='w+b', tempfile=True):
    if fd is None and tempfile:
        fd = TemporaryFile(mode=mode)

    if isinstance(fd, basestring):
        fd = open(fd, mode=mode)

    return fd

def db_to_float(db, using_amplitude=True):
    """
    Converts the input db to a float, which represents the equivalent
    ratio in power.
    """
    db = float(db)
    if using_amplitude:
        return 10 ** (db / 20)
    else: # using power
        return 10 ** (db / 10)

def ratio_to_db(ratio, val2=None, using_amplitude=True):
    """
    Converts the input float to db, which represents the equivalent
    to the ratio in power represented by the multiplier passed in.
    """
    ratio = float(ratio)

    # accept 2 values and use the ratio of val1 to val2
    if val2 is not None:
        ratio = ratio / val2

    # special case for multiply-by-zero (convert to silence)
    if ratio == 0:
        return -float('inf')

    if using_amplitude:
        return 20 * log(ratio, 10)
    else: # using power
        return 10 * log(ratio, 10)

def register_pydub_effect(fn, name=None):
    """
    decorator for adding pydub effects to the AudioSegment objects.

    example use:

        @register_pydub_effect
        def normalize(audio_segment):
            ...

    or you can specify a name:

        @register_pydub_effect("normalize")
        def normalize_audio_segment(audio_segment):
            ...

    """
    if isinstance(fn, basestring):
        name = fn
        return lambda fn: register_pydub_effect(fn, name)

    if name is None:
        name = fn.__name__

    from .audio_segment import AudioSegment
    setattr(AudioSegment, name, fn)
    return fn

def make_chunks(audio_segment, chunk_length):
    """
    Breaks an AudioSegment into chunks that are <chunk_length> milliseconds
    long.

    if chunk_length is 50 then you'll get a list of 50 millisecond long audio
    segments back (except the last one, which can be shorter)
    """
    number_of_chunks = ceil(len(audio_segment) / float(chunk_length))
    return [audio_segment[i * chunk_length:(i + 1) * chunk_length]
            for i in range(int(number_of_chunks))]

def which(program):
    """
    Mimics behavior of UNIX which command.
    """
    #Add .exe program extension for windows support
    if os.name == "nt" and not program.endswith(".exe"):
        program += ".exe"

    envdir_list = [os.curdir] + os.environ["PATH"].split(os.pathsep)

    for envdir in envdir_list:
        program_path = os.path.join(envdir, program)
        if os.path.isfile(program_path) and os.access(program_path, os.X_OK):
            return program_path

def get_encoder_name():
    """
    Return enconder default application for system, either avconv or ffmpeg
    """
    if which("avconv"):
        return "avconv"
    elif which("ffmpeg"):
        return "ffmpeg"
    else:
        # should raise exception
        warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning)
        return "ffmpeg"

def get_player_name():
    """
    Return enconder default application for system, either avconv or ffmpeg
    """
    if which("avplay"):
        return "avplay"
    elif which("ffplay"):
        return "ffplay"
    else:
        # should raise exception
        warn("Couldn't find ffplay or avplay - defaulting to ffplay, but may not work", RuntimeWarning)
        return "ffplay"

def get_prober_name():
    """
    Return probe application, either avconv or ffmpeg
    """
    if which("avprobe"):
        return "avprobe"
    elif which("ffprobe"):
        return "ffprobe"
    else:
        # should raise exception
        warn("Couldn't find ffprobe or avprobe - defaulting to ffprobe, but may not work", RuntimeWarning)
        return "ffprobe"

def mediainfo(filepath):
    """Return dictionary with media info(codec, duration, size, bitrate...) from filepath
    """

    from .audio_segment import AudioSegment

    command = [
        get_prober_name(),
        "-v", "quiet",
        "-show_format",
        "-show_streams",
        filepath
    ]
    output = Popen(command, stdout=PIPE).communicate()[0].decode("utf-8")

    rgx = re.compile(r"(?:(?P<inner_dict>.*?):)?(?P<key>.*?)\=(?P<value>.*?)$")
    info = {}
    for line in output.split("\n"):
        # print(line)
        mobj = rgx.match(line)

        if mobj:
            # print(mobj.groups())
            inner_dict, key, value = mobj.groups()

            if inner_dict:
                try:
                    info[inner_dict]
                except KeyError:
                    info[inner_dict] = {}
                info[inner_dict][key] = value
            else:
                info[key] = value

    return info


text:
package gov.samhsa.consent2share.domain.reference;

import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

@Configurable
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/META-INF/spring/applicationContext*.xml")
@ActiveProfiles("test")
@Transactional
public class ConsentDirectiveTypeCodeIntegrationTest {

    @Test
    public void testMarkerMethod() {
    }

	@Autowired
    ConsentDirectiveTypeCodeDataOnDemand dod;

	@Autowired
    ConsentDirectiveTypeCodeRepository consentDirectiveTypeCodeRepository;

	@Test
    public void testCountAllConsentDirectiveTypeCodes() {
        Assert.assertNotNull("Data on demand for 'ConsentDirectiveTypeCode' failed to initialize correctly", dod.getRandomConsentDirectiveTypeCode());
        long count = consentDirectiveTypeCodeRepository.count();
        Assert.assertTrue("Counter for 'ConsentDirectiveTypeCode' incorrectly reported there were no entries", count > 0);
    }

	@Test
    public void testFindConsentDirectiveTypeCode() {
        ConsentDirectiveTypeCode obj = dod.getRandomConsentDirectiveTypeCode();
        Assert.assertNotNull("Data on demand for 'ConsentDirectiveTypeCode' failed to initialize correctly", obj);
        Long id = obj.getId();
        Assert.assertNotNull("Data on demand for 'ConsentDirectiveTypeCode' failed to provide an identifier", id);
        obj = consentDirectiveTypeCodeRepository.findOne(id);
        Assert.assertNotNull("Find method for 'ConsentDirectiveTypeCode' illegally returned null for id '" + id + "'", obj);
        Assert.assertEquals("Find method for 'ConsentDirectiveTypeCode' returned the incorrect identifier", id, obj.getId());
    }

	@Test
    public void testFindAllConsentDirectiveTypeCodes() {
        Assert.assertNotNull("Data on demand for 'ConsentDirectiveTypeCode' failed to initialize correctly", dod.getRandomConsentDirectiveTypeCode());
        long count = consentDirectiveTypeCodeRepository.count();
        Assert.assertTrue("Too expensive to perform a find all test for 'ConsentDirectiveTypeCode', as there are " + count + " entries; set the findAllMaximum to exceed this value or set findAll=false on the integration test annotation to disable the test", count < 250);
        List<ConsentDirectiveTypeCode> result = consentDirectiveTypeCodeRepository.findAll();
        Assert.assertNotNull("Find all method for 'ConsentDirectiveTypeCode' illegally returned null", result);
        Assert.assertTrue("Find all method for 'ConsentDirectiveTypeCode' failed to return any data", result.size() > 0);
    }

	@Test
    public void testFindConsentDirectiveTypeCodeEntries() {
        Assert.assertNotNull("Data on demand for 'ConsentDirectiveTypeCode' failed to initialize correctly", dod.getRandomConsentDirectiveTypeCode());
        long count = consentDirectiveTypeCodeRepository.count();
        if (count > 20) count = 20;
		int pageNumber = 0;
        int pageSize = 10;
        List<ConsentDirectiveTypeCode> result = consentDirectiveTypeCodeRepository.findAll(
				new PageRequest(pageNumber, pageSize)).getContent();
        Assert.assertNotNull("Find entries method for 'ConsentDirectiveTypeCode' illegally returned null", result);
        Assert.assertEquals("Find entries method for 'ConsentDirectiveTypeCode' returned an incorrect number of entries", count, result.size());
    }

	@Test
    public void testFlush() {
        ConsentDirectiveTypeCode obj = dod.getRandomConsentDirectiveTypeCode();
        Assert.assertNotNull("Data on demand for 'ConsentDirectiveTypeCode' failed to initialize correctly", obj);
        Long id = obj.getId();
        Assert.assertNotNull("Data on demand for 'ConsentDirectiveTypeCode' failed to provide an identifier", id);
        obj = consentDirectiveTypeCodeRepository.findOne(id);
        Assert.assertNotNull("Find method for 'ConsentDirectiveTypeCode' illegally returned null for id '" + id + "'", obj);
        boolean modified =  dod.modifyConsentDirectiveTypeCode(obj);
        Integer currentVersion = obj.getVersion();
        consentDirectiveTypeCodeRepository.flush();
        Assert.assertTrue("Version for 'ConsentDirectiveTypeCode' failed to increment on flush directive", (currentVersion != null && obj.getVersion() > currentVersion) || !modified);
    }

	@Test
    public void testUpdateConsentDirectiveTypeCodeUpdate() {
        ConsentDirectiveTypeCode obj = dod.getRandomConsentDirectiveTypeCode();
        Assert.assertNotNull("Data on demand for 'ConsentDirectiveTypeCode' failed to initialize correctly", obj);
        Long id = obj.getId();
        Assert.assertNotNull("Data on demand for 'ConsentDirectiveTypeCode' failed to provide an identifier", id);
        obj = consentDirectiveTypeCodeRepository.findOne(id);
        boolean modified =  dod.modifyConsentDirectiveTypeCode(obj);
        Integer currentVersion = obj.getVersion();
        ConsentDirectiveTypeCode merged = (ConsentDirectiveTypeCode)consentDirectiveTypeCodeRepository.save(obj);
        consentDirectiveTypeCodeRepository.flush();
        Assert.assertEquals("Identifier of merged object not the same as identifier of original object", merged.getId(), id);
        Assert.assertTrue("Version for 'ConsentDirectiveTypeCode' failed to increment on merge and flush directive", (currentVersion != null && obj.getVersion() > currentVersion) || !modified);
    }

	@Test
    public void testSaveConsentDirectiveTypeCode() {
        Assert.assertNotNull("Data on demand for 'ConsentDirectiveTypeCode' failed to initialize correctly", dod.getRandomConsentDirectiveTypeCode());
        ConsentDirectiveTypeCode obj = dod.getNewTransientConsentDirectiveTypeCode(Integer.MAX_VALUE);
        Assert.assertNotNull("Data on demand for 'ConsentDirectiveTypeCode' failed to provide a new transient entity", obj);
        Assert.assertNull("Expected 'ConsentDirectiveTypeCode' identifier to be null", obj.getId());
        consentDirectiveTypeCodeRepository.save(obj);
        consentDirectiveTypeCodeRepository.flush();
        Assert.assertNotNull("Expected 'ConsentDirectiveTypeCode' identifier to no longer be null", obj.getId());
    }

	@Test
    public void testDeleteConsentDirectiveTypeCode() {
        ConsentDirectiveTypeCode obj = dod.getRandomConsentDirectiveTypeCode();
        Assert.assertNotNull("Data on demand for 'ConsentDirectiveTypeCode' failed to initialize correctly", obj);
        Long id = obj.getId();
        Assert.assertNotNull("Data on demand for 'ConsentDirectiveTypeCode' failed to provide an identifier", id);
        obj = consentDirectiveTypeCodeRepository.findOne(id);
        consentDirectiveTypeCodeRepository.delete(obj);
        consentDirectiveTypeCodeRepository.flush();
        Assert.assertNull("Failed to remove 'ConsentDirectiveTypeCode' with identifier '" + id + "'", consentDirectiveTypeCodeRepository.findOne(id));
    }
}


text:
/* eslint-disable @typescript-eslint/no-explicit-any */
export const httpMock = {
    createServer: (): { on: () => void } => {
        console.log("calling create server");
        return {
            on: (): void => console.log("calling on server")
        };
    }
};

export const httpProxyMock = {
    createProxyServer: (): { on: () => void; ws: () => void; web: () => void } => {
        console.log("calling create proxy server");
        return {
            on: (): void => console.log("calling on proxy"),
            ws: (): void => console.log("calling ws proxy"),
            web: (): void => console.log("calling web proxy")
        };
    }
};

const expressMock = (): { get: () => void; use: () => void } => {
    console.log("calling express");
    return {
        get: (): void => console.log("calling on server"),
        use: (): void => console.log("calling use server")
    };
};

expressMock.static = (): void => console.log("calling static");

export { expressMock };

export const requestMock = {
    get: (): { pipe: () => void } => {
        console.log("calling request get");
        return {
            pipe: (): void => console.log("calling pipe of request")
        };
    }
};

export const concatStreamMock = (): void => console.log("calling concat stream");

export const parserMock = {
    parse: (): Promise<any> => {
        return Promise.resolve<any>({
            serverSettings: {
                disableCache: true,
                verboseLogging: true,
            },
            apps: [
                {
                    url: {
                        base: "localhost:3000",
                        path: "/"
                    },
                    route: "/rc"
                },
                {
                    file: {
                        path: "./src"
                    },
                    route: "/ng"
                }
            ],
            glueAssets: {
                sharedWorker: "./"
            }
        });
    }
};

export const processMock = {
    cwd: (): string => {
        console.log("calling process cwd");
        return "C://Users/flash/Desktop/";
    }
};


text:
<?php

namespace SLLH\IsoCodesValidator\Constraints;

use IsoCodes;
use SLLH\IsoCodesValidator\AbstractIsoCodesConstraintValidator;
use Symfony\Component\Validator\Constraint;

/**
 * @author Sullivan Senechal <soullivaneuh@gmail.com>
 */
final class IpValidator extends AbstractIsoCodesConstraintValidator
{
    /**
     * {@inheritdoc}
     */
    public function validate($value, Constraint $constraint)
    {
        parent::validate($value, $constraint);

        if ($value && !IsoCodes\IP::validate($value) && !IsoCodes\IP::validateIPV6($value)) {
            $this->createViolation($constraint->message);
        }
    }
}


text:
namespace Microsoft.Azure.Management.FrontDoor.Models
{

    /// <summary>
    /// Defines values for ManagedRuleSetActionType.
    /// </summary>
    public static class ManagedRuleSetActionType
    {
        public const string Block = "Block";
        public const string Log = "Log";
        public const string Redirect = "Redirect";
    }
}


text:
<TS language="ms_MY" version="2.1">
<context>
    <name>AcceptandPayOfferListPage</name>
    </context>
<context>
    <name>AcceptedOfferListPage</name>
    <message>
        <source>Export the data in the current tab to a file</source>
        <translation>
Alihkan fail data ke dalam tab semasa</translation>
    </message>
    <message>
        <source>Comma separated file (*.csv)</source>
        <translation>Fail yang dipisahkan dengan koma</translation>
    </message>
    </context>
<context>
    <name>AddressBookPage</name>
    <message>
        <source>Create a new address</source>
        <translation>Cipta alamat baru</translation>
    </message>
    <message>
        <source>&New</source>
        <translation>&Baru</translation>
    </message>
    <message>
        <source>Copy the currently selected address to the system clipboard</source>
        <translation>Salin alamat terpilih ke dalam sistem papan klip</translation>
    </message>
    <message>
        <source>&Copy</source>
        <translation>&Salin</translation>
    </message>
    <message>
        <source>Delete the currently selected address from the list</source>
        <translation>Padam alamat semasa yang dipilih dari senaraiyang dipilih dari senarai</translation>
    </message>
    <message>
        <source>Export the data in the current tab to a file</source>
        <translation>
Alihkan fail data ke dalam tab semasa</translation>
    </message>
    <message>
        <source>&Export</source>
        <translation>&Eksport</translation>
    </message>
    <message>
        <source>&Delete</source>
        <translation>&Padam</translation>
    </message>
    <message>
        <source>Choose the address to send coins to</source>
        <translation>Pilih alamat untuk menghantar syiling</translation>
    </message>
    <message>
        <source>Choose the address to receive coins with</source>
        <translation>Pilih alamat untuk menerima syiling</translation>
    </message>
    <message>
        <source>C&hoose</source>
        <translation>&Pilih</translation>
    </message>
    <message>
        <source>&Copy Address</source>
        <translation>&Salin Alamat</translation>
    </message>
    <message>
        <source>Comma separated file (*.csv)</source>
        <translation>Fail yang dipisahkan dengan koma</translation>
    </message>
    <message>
        <source>Address</source>
        <translation>Alamat</translation>
    </message>
    </context>
<context>
    <name>AddressTableModel</name>
    <message>
        <source>Address</source>
        <translation>Alamat</translation>
    </message>
    </context>
<context>
    <name>AliasListPage</name>
    </context>
<context>
    <name>AliasTableModel</name>
    </context>
<context>
    <name>AliasView</name>
    </context>
<context>
    <name>AskPassphraseDialog</name>
    </context>
<context>
    <name>BanTableModel</name>
    </context>
<context>
    <name>CertListPage</name>
    </context>
<context>
    <name>CertTableModel</name>
    </context>
<context>
    <name>CertView</name>
    </context>
<context>
    <name>CoinControlDialog</name>
    </context>
<context>
    <name>EditAddressDialog</name>
    <message>
        <source>Edit Address</source>
        <translation>Alamat</translation>
    </message>
    <message>
        <source>&Address</source>
        <translation>Alamat</translation>
    </message>
    </context>
<context>
    <name>EditAliasDialog</name>
    </context>
<context>
    <name>EditCertDialog</name>
    </context>
<context>
    <name>EditOfferDialog</name>
    </context>
<context>
    <name>EditWhitelistOfferDialog</name>
    <message>
        <source>Export the data in the current tab to a file</source>
        <translation>
Alihkan fail data ke dalam tab semasa</translation>
    </message>
    <message>
        <source>Comma separated file (*.csv)</source>
        <translation>Fail yang dipisahkan dengan koma</translation>
    </message>
    </context>
<context>
    <name>EscrowInfoDialog</name>
    </context>
<context>
    <name>EscrowListPage</name>
    </context>
<context>
    <name>EscrowTableModel</name>
    </context>
<context>
    <name>EscrowView</name>
    </context>
<context>
    <name>FreespaceChecker</name>
    </context>
<context>
    <name>HelpMessageDialog</name>
    </context>
<context>
    <name>InMessageListPage</name>
    <message>
        <source>Comma separated file (*.csv)</source>
        <translation>Fail yang dipisahkan dengan koma</translation>
    </message>
    </context>
<context>
    <name>Intro</name>
    </context>
<context>
    <name>ManageEscrowDialog</name>
    </context>
<context>
    <name>MessageInfoDialog</name>
    </context>
<context>
    <name>MessageListPage</name>
    <message>
        <source>Export the data in the current tab to a file</source>
        <translation>
Alihkan fail data ke dalam tab semasa</translation>
    </message>
</context>
<context>
    <name>MessageTableModel</name>
    </context>
<context>
    <name>MessageView</name>
    </context>
<context>
    <name>ModalOverlay</name>
    </context>
<context>
    <name>MyAcceptedOfferListPage</name>
    <message>
        <source>Export the data in the current tab to a file</source>
        <translation>
Alihkan fail data ke dalam tab semasa</translation>
    </message>
    <message>
        <source>Comma separated file (*.csv)</source>
        <translation>Fail yang dipisahkan dengan koma</translation>
    </message>
    </context>
<context>
    <name>MyAliasListPage</name>
    <message>
        <source>Export the data in the current tab to a file</source>
        <translation>
Alihkan fail data ke dalam tab semasa</translation>
    </message>
    <message>
        <source>Comma separated file (*.csv)</source>
        <translation>Fail yang dipisahkan dengan koma</translation>
    </message>
    </context>
<context>
    <name>MyCertListPage</name>
    <message>
        <source>Export the data in the current tab to a file</source>
        <translation>
Alihkan fail data ke dalam tab semasa</translation>
    </message>
    <message>
        <source>Comma separated file (*.csv)</source>
        <translation>Fail yang dipisahkan dengan koma</translation>
    </message>
    </context>
<context>
    <name>MyEscrowListPage</name>
    <message>
        <source>Export the data in the current tab to a file</source>
        <translation>
Alihkan fail data ke dalam tab semasa</translation>
    </message>
    <message>
        <source>Comma separated file (*.csv)</source>
        <translation>Fail yang dipisahkan dengan koma</translation>
    </message>
    </context>
<context>
    <name>MyOfferListPage</name>
    <message>
        <source>Export the data in the current tab to a file</source>
        <translation>
Alihkan fail data ke dalam tab semasa</translation>
    </message>
    <message>
        <source>Comma separated file (*.csv)</source>
        <translation>Fail yang dipisahkan dengan koma</translation>
    </message>
    </context>
<context>
    <name>MyOfferWhitelistTableModel</name>
    </context>
<context>
    <name>MyWhitelistOfferDialog</name>
    <message>
        <source>Export the data in the current tab to a file</source>
        <translation>
Alihkan fail data ke dalam tab semasa</translation>
    </message>
    <message>
        <source>Comma separated file (*.csv)</source>
        <translation>Fail yang dipisahkan dengan koma</translation>
    </message>
    </context>
<context>
    <name>NewMessageDialog</name>
    </context>
<context>
    <name>NewWhitelistDialog</name>
    </context>
<context>
    <name>OfferAcceptDialog</name>
    </context>
<context>
    <name>OfferAcceptDialogBTC</name>
    </context>
<context>
    <name>OfferAcceptDialogZEC</name>
    </context>
<context>
    <name>OfferAcceptInfoDialog</name>
    </context>
<context>
    <name>OfferAcceptTableModel</name>
    </context>
<context>
    <name>OfferEscrowDialog</name>
    </context>
<context>
    <name>OfferFeedbackDialog</name>
    </context>
<context>
    <name>OfferListPage</name>
    </context>
<context>
    <name>OfferPayDialog</name>
    </context>
<context>
    <name>OfferTableModel</name>
    </context>
<context>
    <name>OfferView</name>
    </context>
<context>
    <name>OfferWhitelistTableModel</name>
    </context>
<context>
    <name>OpenURIDialog</name>
    </context>
<context>
    <name>OptionsDialog</name>
    </context>
<context>
    <name>OptionsModel</name>
    </context>
<context>
    <name>OutMessageListPage</name>
    <message>
        <source>Comma separated file (*.csv)</source>
        <translation>Fail yang dipisahkan dengan koma</translation>
    </message>
    </context>
<context>
    <name>OverviewPage</name>
    </context>
<context>
    <name>PaymentServer</name>
    </context>
<context>
    <name>PeerTableModel</name>
    </context>
<context>
    <name>QObject</name>
    </context>
<context>
    <name>QRImageWidget</name>
    </context>
<context>
    <name>RPCConsole</name>
    </context>
<context>
    <name>ReceiveCoinsDialog</name>
    </context>
<context>
    <name>ReceiveRequestDialog</name>
    <message>
        <source>Copy &Address</source>
        <translation>&Salin Alamat</translation>
    </message>
    <message>
        <source>Address</source>
        <translation>Alamat</translation>
    </message>
    </context>
<context>
    <name>RecentRequestsTableModel</name>
    </context>
<context>
    <name>ResellOfferDialog</name>
    </context>
<context>
    <name>SendCoinsDialog</name>
    <message>
        <source>Balance:</source>
        <translation>Baki</translation>
    </message>
    </context>
<context>
    <name>SendCoinsEntry</name>
    </context>
<context>
    <name>SendConfirmationDialog</name>
    </context>
<context>
    <name>ShutdownWindow</name>
    </context>
<context>
    <name>SignRawTxDialog</name>
    </context>
<context>
    <name>SignVerifyMessageDialog</name>
    </context>
<context>
    <name>SplashScreen</name>
    </context>
<context>
    <name>GtacoinGUI</name>
    <message>
        <source>&Options...</source>
        <translation>Pilihan</translation>
    </message>
    </context>
<context>
    <name>TrafficGraphWidget</name>
    </context>
<context>
    <name>TransactionDesc</name>
    </context>
<context>
    <name>TransactionDescDialog</name>
    </context>
<context>
    <name>TransactionTableModel</name>
    </context>
<context>
    <name>TransactionView</name>
    <message>
        <source>Comma separated file (*.csv)</source>
        <translation>Fail yang dipisahkan dengan koma</translation>
    </message>
    <message>
        <source>Address</source>
        <translation>Alamat</translation>
    </message>
    </context>
<context>
    <name>UnitDisplayStatusBarControl</name>
    </context>
<context>
    <name>WalletFrame</name>
    </context>
<context>
    <name>WalletModel</name>
    </context>
<context>
    <name>WalletView</name>
    <message>
        <source>Export the data in the current tab to a file</source>
        <translation>
Alihkan fail data ke dalam tab semasa</translation>
    </message>
    </context>
<context>
    <name>gtacoin-core</name>
    </context>
</TS>