mirror of
https://github.com/hanzoai/psrpc.git
synced 2026-09-30 23:47:22 +00:00
PSRPC for Hanzo LiveKit, forked from livekit/psrpc (Apache-2.0). Two rewrites: github.com/livekit/psrpc -> github.com/hanzoai/psrpc github.com/redis/go-redis/v9 -> github.com/hanzokv/go/v9 psrpc's message bus (bus.go, internal/bus/bus_redis.go, internal/bus/bustest) speaks to a KV over go-redis. After hanzoai/livekit moved to our client, psrpc was the one remaining edge pulling upstream go-redis into that binary — not our code, but our dependency, the same shape redsync had for hanzoai/git. The other livekit/* modules (protocol, mediatransportutil, mageutil) are NOT forked and should not be: none of them touch a KV client, so they are ordinary external dependencies and their names are addresses.
128 lines
2.7 KiB
Go
128 lines
2.7 KiB
Go
// Copyright 2023 LiveKit, Inc.
|
|
//
|
|
// Licensed 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 stream
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"go.uber.org/atomic"
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"github.com/hanzoai/psrpc"
|
|
"github.com/hanzoai/psrpc/internal"
|
|
"github.com/hanzoai/psrpc/pkg/info"
|
|
"github.com/hanzoai/psrpc/pkg/rand"
|
|
)
|
|
|
|
func TestClosePendingSend(t *testing.T) {
|
|
s := NewStream[*internal.Request, *internal.Response](
|
|
context.Background(),
|
|
&info.RequestInfo{},
|
|
rand.NewStreamID(),
|
|
psrpc.DefaultClientTimeout,
|
|
&testStreamAdapter{},
|
|
nil,
|
|
make(chan *internal.Response),
|
|
make(map[string]chan struct{}),
|
|
)
|
|
|
|
var err atomic.Value
|
|
ready := make(chan struct{})
|
|
|
|
var wg sync.WaitGroup
|
|
wg.Add(2)
|
|
go func() {
|
|
close(ready)
|
|
err.Store(s.Send(&internal.Request{}))
|
|
wg.Done()
|
|
}()
|
|
|
|
var handleErr error
|
|
go func() {
|
|
<-ready
|
|
handleErr = s.HandleStream(&internal.Stream{
|
|
Body: &internal.Stream_Close{
|
|
Close: &internal.StreamClose{
|
|
Error: psrpc.ErrStreamClosed.Error(),
|
|
Code: string(psrpc.ErrStreamClosed.Code()),
|
|
},
|
|
},
|
|
})
|
|
wg.Done()
|
|
}()
|
|
wg.Wait()
|
|
|
|
require.NoError(t, handleErr)
|
|
require.EqualValues(t, psrpc.ErrStreamClosed, err.Load())
|
|
}
|
|
|
|
type testStreamAdapter struct {
|
|
sendCalls atomic.Int32
|
|
closeCalls atomic.Int32
|
|
}
|
|
|
|
func (a *testStreamAdapter) Send(ctx context.Context, msg *internal.Stream) error {
|
|
a.sendCalls.Inc()
|
|
return nil
|
|
}
|
|
|
|
func (a *testStreamAdapter) Close(streamID string) {
|
|
a.closeCalls.Inc()
|
|
}
|
|
|
|
func TestClosePanic(t *testing.T) {
|
|
for i := 0; i < 1000; i++ {
|
|
s := NewStream[*internal.Request, *internal.Response](
|
|
context.Background(),
|
|
&info.RequestInfo{},
|
|
rand.NewStreamID(),
|
|
psrpc.DefaultClientTimeout,
|
|
&testStreamAdapter{},
|
|
nil,
|
|
make(chan *internal.Response, 1),
|
|
make(map[string]chan struct{}),
|
|
)
|
|
|
|
ready := make(chan struct{})
|
|
var wg sync.WaitGroup
|
|
wg.Add(2)
|
|
|
|
go func() {
|
|
close(ready)
|
|
s.Close(nil)
|
|
wg.Done()
|
|
}()
|
|
|
|
go func() {
|
|
b, _ := proto.Marshal(&internal.Response{})
|
|
|
|
<-ready
|
|
s.HandleStream(&internal.Stream{
|
|
Body: &internal.Stream_Message{
|
|
Message: &internal.StreamMessage{
|
|
RawMessage: b,
|
|
},
|
|
},
|
|
})
|
|
wg.Done()
|
|
}()
|
|
|
|
wg.Wait()
|
|
}
|
|
}
|