-
Notifications
You must be signed in to change notification settings - Fork 166
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
feat: 添加 MultipleBytes 实现多协程安全读写 #272
base: dev
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
把线程安全的特征删掉,因为主要使用场景是单一 goroutine 内部。
而后使用装饰器模式额外提供一个线程安全的版本,你随便命名。
暴露一个 New 方法,参数是预期中的 slice 的数量,用于初始化你的 data 的容量。
iox/multiple_bytes.go
Outdated
func (m *MultipleBytes) Len() int { | ||
m.mutex.RLock() | ||
defer m.mutex.RUnlock() | ||
|
||
total := 0 | ||
for _, slice := range m.data { | ||
total += len(slice) | ||
} | ||
return total | ||
} | ||
|
||
// Cap 返回当前缓冲区的容量 | ||
func (m *MultipleBytes) Cap() int { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这两个方法先不需要实现,因为目前来看没什么使用场景,而且 Cap 没有啥意义,因为是允许扩容的
iox/multiple_bytes.go
Outdated
} | ||
|
||
// Bytes 返回内部缓冲区的副本 | ||
func (m *MultipleBytes) Bytes() []byte { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个也不需要
iox/multiple_bytes.go
Outdated
} | ||
|
||
// Clear 清空缓冲区并重置读取位置 | ||
func (m *MultipleBytes) Clear() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同样不需要
iox/multiple_bytes_test.go
Outdated
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMultipleBytes_ReadWrite(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Write 测试用例比较简单,但是 Read 需要更加复杂的测试用例
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Read 要考虑更加多的边缘场景,包括但不限于:
- 只有一个 slice,读够了,没读够;
- 多个 slice,读数据横跨了多个 slice 才读够、恰好读够,没读够;
- idx1 和 idx2 指向第一个、最好一个的交叉验证;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
并发测试不用那么麻烦,我们基本上通过代码 review 来确认加锁解锁没有问题。
而且这么复杂的测试,我看上去也没啥效果,所以可以删了。
或者说,并发版本你只要保证代码覆盖率就可以了
iox/multiple_bytes.go
Outdated
return 0, nil | ||
} | ||
|
||
// 创建新的切片来存储数据 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不需要,我们就是为了规避 copy 而引入的,所以这里不要 copy,直接 append
// 读取数据 | ||
for i, size := range tc.readSizes { | ||
read := make([]byte, size) | ||
n, err := mb.Read(read) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个 n 也要校验,确保你在代码里面是计算对了
} | ||
|
||
// 测试基本的并发读写功能 | ||
func TestConcurrentMultipleBytesConcurrentReadWrite(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个不需要重复测试,因为本身你可以认为非线程安全的版本已经保证这一段是正确的了,所以实际上你只需要随便写一点测试就可以。
No description provided.